169. 多数元素

 1 class Solution 
 2 {
 3 public:
 4     int majorityElement(vector<int>& nums) 
 5     {
 6         unordered_map<int,int> hash;
 7         for(auto a : nums) hash[a]++;
 8         for(auto a : hash) 
 9         {
10             if(a.second > nums.size()/2) return a.first;
11         }
12         return -1;
13     }
14 };

 

posted @ 2020-04-03 16:34  Jinxiaobo0509  阅读(106)  评论(0)    收藏  举报