#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;
}
}
};
时间: 2024-10-10 02:25:07