标签:
#include<map>
using namespace std;
class Solution {
public:
int majorityElement(vector<int>& nums) {
map<int,int> m;
int n=nums.size();
int i=0;
while(i<nums.size()){
if(m.count(nums[i])) m[nums[i]]++;
else m.insert(pair<int,int>(nums[i],1));
i++;
}
i=0;
map <int, int>::iterator m1_Iter;
for ( m1_Iter = m.begin( ); m1_Iter != m.end( ); m1_Iter++ ){
if(m1_Iter->second>n/2)
return m1_Iter->first;
}
}
};
标签:
原文地址:http://www.cnblogs.com/julie-yang/p/4652649.html