Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋
times. The algorithm should run in linear time and in O(1) space.
Hint:
- How many majority elements could it possibly have?
- Do you have a better hint? Suggest it!
解法一:
Moore voting的方法。
class Solution {
public:
vector<int> majorityElement(vector<int>& nums) {
vector<int> res;
int m=0, n=0, cm=0, cn=0;
for(auto &a:nums){
if(a==m) ++cm;
else if(a==n) ++cn;
else if(cm==0) m=a, cm=1;
else if(cn==0) n=a, cn=1;
else --cm, --cn;
}
cm=0; cn=0;
for(auto &a:nums){
if(a==m) cm++;
else if(a==n) cn++;
}
if(cm>nums.size()/3) res.push_back(m);
if(cn>nums.size()/3) res.push_back(n);
return res;
}
};