摩尔投票法
class Solution {
public:
int majorityElement(vector<int>& nums) {
int n = nums.size();
int i = 1;
int candi = nums[0], count = 1;
while(i < n){
if(nums[i] == candi) count++;
else if(nums[i] != candi && count == 0){
candi = nums[i];
count++;
}else{
count--;
}
++i;
}
return candi;
}
};