标签:
https://leetcode.com/problems/majority-element/
1 class Solution { 2 public: 3 int majorityElement(vector<int>& nums) { 4 map<int,int> mymap; 5 map<int,int>::iterator it; 6 int n=nums.size(); 7 for(int i=0;i<n;i++) 8 { 9 it=mymap.find(nums[i]); 10 if(it==mymap.end()) 11 { 12 mymap.insert(pair<int,int>(nums[i],1)); 13 } 14 else 15 { 16 (*it).second++; 17 } 18 } 19 int max=0; 20 int maxnum; 21 for ( it=mymap.begin() ; it != mymap.end(); it++ ) 22 { 23 if((*it).second>max) 24 { 25 max=(*it).second; 26 maxnum=(*it).first; 27 } 28 29 } 30 return maxnum; 31 } 32 };
标签:
原文地址:http://www.cnblogs.com/aguai1992/p/4629596.html