题目描述:
首刷看解:
class Solution {
public:
vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
if (nums1.size() > nums2.size())
return intersect(nums2, nums1);
unordered_map<int, int>buckets;
vector<int> intersection;
for (const auto& iter1 : nums1)
++buckets[iter1];
for (const auto& iter2 : nums2)
{
if (buckets.count(iter2))
{
intersection.push_back(iter2);
--buckets[iter2];
if (!buckets[iter2])
buckets.erase(iter2);
}
}
return intersection;
}
};