为什么我要写这个题目,因为这个题目真的很棒。谁想出来的 ,厉害呀
public int MoreThanHalfNum_Solution(int [] array) {
int res = array[0];
int count = 1;
//然后我们遍历这个数组
for (int i = 1; i < array.length; i++) {
if (count == 0) {
res = array[i];
count++;
continue;
}
if (res == array[i]) {
count++;
} else {
count--;
}
}
//if(count == 0) return 0;
count = 0;
for (int i=0; i<array.length; i++){
if(array[i] == res){
count ++ ;
}
}
return (count > array.length/2) ? res : 0;
}