349.两个数组的交集
题目链接
static const auto io_speed_up = []()
{
ios::sync_with_stdio(0);
cin.tie(0);
return 0;
}();
class Solution {
public:
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
unordered_set<int>ms1(nums1.begin(),nums1.end());
vector<int>ans;
for(auto i : nums2)if(ms1.erase(i))ans.emplace_back(i);
return ans;
}
};
350.两个数组的交集 II
题目链接
static const auto io_speed_up = []()
{
ios::sync_with_stdio(0);
cin.tie(0);
return 0;
}();
class Solution {
public:
vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
unordered_multiset<int>ms1(nums1.begin(),nums1.end());
vector<int>ans;
unordered_multiset<int>::iterator it;
for(auto i : nums2){
it = ms1.find(i);
if(it!=ms1.end()){
ans.emplace_back(i);
ms1.erase(it);
}
}
return ans;
}
};