Algorithm2: 重复查过半数的元素

数组中,有一个元素的值在数组中重复的个数是超过一半,获得元素超过一半的元素值
int MoreThanHalfNumber(int * arr, int n){
                 int result = arr [0];
                 int time = 1;
                 for (int i = 1; i < n; i++){
                                 if (time == 0){
                                                result = arr[i];
                                                time = 1;
                                }
                                 else if (arr[i] == result){
                                                time++;
                                }
                                 else{
                                                time--;
                                }
                }
                 return result;
}
上一篇:集成学习—boosting和bagging异同


下一篇:Unity 性能