Given two arrays, write a function to compute their intersection.
Example:
Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2].
Note:
- Each element in the result should appear as many times as it shows in both arrays.
- The result can be in any order.
Follow up:
- What if the given array is already sorted? How would you optimize your algorithm?
- What if nums1's size is small compared to num2's size? Which algorithm is better?
- What if elements of nums2 are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once?
给定两个数组,返回它们的共有部分,其中重复的元素也重复返回。
【思路1】
将两个数组排序,然后顺序遍历并比较。
时间复杂度 O(max(m, n) log(max(m, n))) ,空间复杂度O(m + n)
class Solution {
public:
vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
sort(nums1.begin(), nums1.end());
sort(nums2.begin(), nums2.end());
int n1 = (int)nums1.size(), n2 = (int)nums2.size();
int i1 = 0, i2 = 0;
vector<int> res;
while(i1 < n1 && i2 < n2){
if(nums1[i1] == nums2[i2]) {
res.push_back(nums1[i1]);
i1++;
i2++;
}
else if(nums1[i1] > nums2[i2]){
i2++;
}
else{
i1++;
}
}
return res;
}
};
【思路2】
利用关联容器map统计数字的数量。
时间复杂度 O(m + n),空间复杂度O(m + n)。
class Solution {
public:
vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
unordered_map<int, int> dict;
vector<int> res;
for(int i = 0; i < (int)nums1.size(); i++) dict[nums1[i]]++;
for(int i = 0; i < (int)nums2.size(); i++)
if(--dict[nums2[i]] >= 0) res.push_back(nums2[i]);
return res;
}
};