Given two arrays, write a function to compute their intersection.
Example:
Given nums1 = [1, 2, 2, 1]
, nums2 = [2, 2]
, return [2]
.
Note:
- Each element in the result must be unique.
- The result can be in any order.
翻译:给定两个数组,写一个函数来提取两个数组的交集,交集中数字只出现一次。
class Solution {
public:
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
vector<int> result;
if(!nums1.empty() &&!nums2.empty())
{
unordered_map<int,bool> m;
for(int i=0;i<nums1.size();i++)
{
m[nums1[i]]=true;
}
for(int i=0;i<nums2.size();i++)
{
if(m[nums2[i]])
{
result.push_back(nums2[i]);
m[nums2[i]]=false;
}
}
}
return result;
}
};