349 https://leetcode.com/problems/intersection-of-two-arrays/
Given two arrays, write a function to compute their intersection.
Example:
Given nums1 = [1, 2, 2, 1]
, nums2 = [2, 2]
, return [2]
.
Note:
这是一道有陷阱的简单题
方法1
代码用了两个set存放原始num,为的是去掉重复元素。如果不适用set2,会存在重复元素的问题
vector intersection(vector& nums1, vector& nums2)
{
unordered_set set1,set2;
for (int n : nums1)
set1.insert(n);
for (int n : nums2)
set2.insert(n);
vector res;
for (int n : set2)
{
if (!set1.insert(n).second)
res.push_back(n);
}
return res;
}
排序后使用双指针
vector intersection(vector& nums1, vector& nums2)
{
int n1 = nums1.size(), n2 = nums2.size(),i=0,j=0;
vector res;
if (n1 == 0 || n2 == 0)
return res;
sort(nums1.begin(), nums1.end());
sort(nums2.begin(), nums2.end());
while (i nums2[j])
j++;
else
{
if (find(res.begin(), res.end(), nums1[i]) == res.end())
res.push_back(nums1[i]);
i++;
j++;
}
}
return res;
}
二分查找
bool search(vector nums, int n)
{
int l = 0, r = nums.size() - 1, m;
while (l <= r)
{
m = (l + r) / 2;
if (nums[m] < n)
l = m + 1;
else if (nums[m] > n)
r = m - 1;
else
return true;
}
return false;
}
vector intersection(vector& nums1, vector& nums2)
{
int n1 = nums1.size(), n2 = nums2.size();
vector res;
if (n1 == 0 || n2 == 0)
return res;
sort(nums1.begin(), nums1.end());
for (int n : nums2)
{
if (search(nums1,n)&& find(res.begin(), res.end(), n) == res.end())
res.push_back(n);
}
return res;
}
350 https://leetcode.com/problems/intersection-of-two-arrays-ii/
Given two arrays, write a function to compute their intersection.
Example:
Given nums1 = [1, 2, 2, 1]
, nums2 = [2, 2]
, return [2, 2]
.
Note:
Follow up:
方法1
直接在nums1中查找每一个nums2元素,如果存在就加入到结果列表,并在nums1中删除当前元素
vector intersect(vector& nums1, vector& nums2)
{
int n1 = nums1.size(), n2 = nums2.size();
vector res;
if (n1 == 0 || n2 == 0)
return res;
for (int n : nums2)
{
auto i = find(nums1.begin(), nums1.end(), n);
if ( i != nums1.end())
{
res.push_back(n);
nums1.erase(i);
}
}
return res;
}
同349一样用双指针,同时不用判断是否存在在结果列表中
vector intersect(vector& nums1, vector& nums2)
{
int n1 = nums1.size(), n2 = nums2.size(), i = 0, j = 0;
vector res;
if (n1 == 0 || n2 == 0)
return res;
sort(nums1.begin(), nums1.end());
sort(nums2.begin(), nums2.end());
while (i < n1&&j < n2)
{
if (nums1[i] < nums2[j])
i++;
else if (nums1[i] > nums2[j])
j++;
else
{
res.push_back(nums1[i]);
i++;
j++;
}
}
return res;
}
计数法 空间消耗O(m),如果不判断是否存在,则消耗O(m + n)。说明参考 https://leetcode.com/discuss/103787/table-solution-pointers-solution-with-time-space-complexity
Based on C++ map mechanism, if a key is not exist, access the key will assign a default value to the key. so if you simply test if map[key] is 0 or not by using "if (map[key] == 0)" without testing if the key is in the map. you will consume extra space....you could avoid allocate extra space either by find or count method. I usually use count, it is more concise. .
vector intersect(vector& nums1, vector& nums2)
{
int n1 = nums1.size(), n2 = nums2.size();
vector res;
if (n1 == 0 || n2 == 0)
return res;
unordered_map dict;
for (int n : nums1)
dict[n]++;
for (int n : nums2)
if (dict.find(n) != dict.end() && --dict[n] >= 0)
res.push_back(n);
return res;
}