代码如下:
public class Solution {
public int MoreThanHalfNum_Solution(int [] array) {
int len = array.length;
int x = 0, votes = 0;
for(int num: array) {
if(votes == 0){
x = num;
}
votes += x == num ? 1 : -1;
}
// Verify
int count = 0;
for(int num: array){
if(num == x){
count++;
}
}
return 2 * count > len ? x : 0;
}
}