O(n) 时间复杂度,题目说了肯定有解
public class Solution {
public int MoreThanHalfNum_Solution(int [] array) {
int res = 0, vote = 0;
for (int i = 0; i < array.length; i ++ ) {
if (vote == 0) {
res = array[i];
vote ++ ;
} else if (res == array[i]) {
vote ++ ;
} else if (res != array[i]) {
vote -- ;
}
}
return res;
}
}

京公网安备 11010502036488号