public class Solution {
public int MoreThanHalfNum_Solution(int [] array) {
int count = 1;
int target = array[0];
for (int i = 1; i < array.length; i++) {
if (target == array[i]) {
count++;
} else {
count--;
//数字出现的次数超过数组长度的一半 最后count一定不为0 所以剩下的肯定时那个数
if (count == 0) {
target = array[i];
count = 1;
}
}
}
return target;
}
} 


京公网安备 11010502036488号