350. Intersection of Two Arrays II

Posted 高数考了59

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了350. Intersection of Two Arrays II相关的知识,希望对你有一定的参考价值。

这个题方法相当多,肉眼可见的有三种

第一种,用set

 1 class Solution 
 2 {
 3 public:
 4     vector<int> intersect(vector<int>& nums1, vector<int>& nums2) 
 5     {
 6         vector<int> res;
 7         unordered_multiset<int> is(nums1.begin(),nums1.end());
 8         for(int i : nums2)
 9         {
10             auto p=is.find(i);
11             if(p!=is.end())
12             {
13                 res.push_back(i);
14                 is.erase(p);
15             }
16         }            
17         return res;
18     }
19 };

第二种,用map

第三种,对俩数组排序,扫描一遍即可。

以上是关于350. Intersection of Two Arrays II的主要内容,如果未能解决你的问题,请参考以下文章