思路展示
示例代码
class Solution {
public:
int majorityElement(vector<int>& nums) {
int len=nums.size();
int candidate=INT_MIN,count=0;
for(int i=0;i<len;i++){
if(count==0){
candidate=nums[i];
}
count+=nums[i]==candidate?1:-1;
}
return candidate;
}
};
效果展示