class Solution {
public int majorityElement(int[] nums) {
int major=0,count=0;
for(int x:nums){
if(count==0){
major=x;
++count;
}else{
if(x==major){
++count;
}else{
--count;
}
}
}
int temp=0;
if(count>0){
for(int x:nums){
if(x==major){
++temp;
}
}
if(temp>nums.length/2)
return major;
}
return -1;
}
}
一天一个新知识,摩尔投票法get