350. 两个数组的交集 II

 1 class Solution 
 2 {
 3 public:
 4     vector<int> intersect(vector<int>& nums1, vector<int>& nums2) 
 5     {
 6         vector<int> res;
 7         unordered_map<int,int> hash1,hash2;
 8         for(auto a : nums1) hash1[a] ++;
 9         for(auto a : nums2) hash2[a] ++;
10         for(auto a : hash1)
11         {
12             if(hash2.count(a.first)) 
13             {
14                 int count = min(a.second,hash2[a.first]);
15                 while(count --) res.push_back(a.first);
16             }
17         }
18         return res;
19     }
20 };

 

posted @ 2020-04-22 19:31  Jinxiaobo0509  阅读(96)  评论(0)    收藏  举报