Leetcode-229 Majority Element II(求众数 II)

 1 class Solution
 2 {
 3     public:
 4         vector<int> majorityElement(vector<int>& nums)
 5         {
 6             unordered_map<int,int> mymap;
 7             vector<int> result;
 8             for(auto d:nums)
 9             {
10                 auto ptr = mymap.find(d);
11                 if(ptr==mymap.end())
12                 {
13                     mymap[d] = 1;
14                 }
15                 else
16                 {
17                     ptr->second ++;
18                 }
19             }
20             int least_times = nums.size()/3;
21             for(auto p:mymap)
22             {
23                 if(p.second>least_times)
24                 {
25                     result.emplace_back(p.first);
26                 }
27             }
28             return result;
29         }
30 };

 

posted @ 2018-08-15 11:14  Asurudo  阅读(155)  评论(0编辑  收藏  举报