LeetCode-349&350.Intersection of Two Arrays
2016-06-13 11:17
288 查看
349 https://leetcode.com/problems/intersection-of-two-arrays/
Given two arrays, write a function to compute their intersection.
Example:
Given nums1 =
Note:
Each element in the result must be unique.
The result can be in any order.
这是一道有陷阱的简单题
方法1
代码用了两个set存放原始num,为的是去掉重复元素。如果不适用set2,会存在重复元素的问题
方法2
排序后使用双指针
二分查找
350 https://leetcode.com/problems/intersection-of-two-arrays-ii/
Given two arrays, write a function to compute their intersection.
Example:
Given nums1 =
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 nums2'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
直接在nums1中查找每一个nums2元素,如果存在就加入到结果列表,并在nums1中删除当前元素
vector<int> intersect(vector<int>& nums1, vector<int>& nums2)
{
int n1 = nums1.size(), n2 = nums2.size();
vector<int> 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;
}
方法2
同349一样用双指针,同时不用判断是否存在在结果列表中
vector<int> intersect(vector<int>& nums1, vector<int>& nums2)
{
int n1 = nums1.size(), n2 = nums2.size(), i = 0, j = 0;
vector<int> 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;
}
方法3
计数法 空间消耗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<int> intersect(vector<int>& nums1, vector<int>& nums2)
{
int n1 = nums1.size(), n2 = nums2.size();
vector<int> res;
if (n1 == 0 || n2 == 0)
return res;
unordered_map<int, int> dict;
for (int n : nums1)
dict
++;
for (int n : nums2)
if (dict.find(n) != dict.end() && --dict
>= 0)
res.push_back(n);
return res;
}
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.
这是一道有陷阱的简单题
方法1
代码用了两个set存放原始num,为的是去掉重复元素。如果不适用set2,会存在重复元素的问题
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) { unordered_set<int> set1,set2; for (int n : nums1) set1.insert(n); for (int n : nums2) set2.insert(n); vector<int> res; for (int n : set2) { if (!set1.insert(n).second) res.push_back(n); } return res; }
方法2
排序后使用双指针
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) { int n1 = nums1.size(), n2 = nums2.size(),i=0,j=0; vector<int> 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 { if (find(res.begin(), res.end(), nums1[i]) == res.end()) res.push_back(nums1[i]); i++; j++; } } return res; }
二分查找
bool search(vector<int> 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<int> intersection(vector<int>& nums1, vector<int>& nums2) { int n1 = nums1.size(), n2 = nums2.size(); vector<int> 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:
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 nums2'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
直接在nums1中查找每一个nums2元素,如果存在就加入到结果列表,并在nums1中删除当前元素
vector<int> intersect(vector<int>& nums1, vector<int>& nums2)
{
int n1 = nums1.size(), n2 = nums2.size();
vector<int> 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;
}
方法2
同349一样用双指针,同时不用判断是否存在在结果列表中
vector<int> intersect(vector<int>& nums1, vector<int>& nums2)
{
int n1 = nums1.size(), n2 = nums2.size(), i = 0, j = 0;
vector<int> 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;
}
方法3
计数法 空间消耗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<int> intersect(vector<int>& nums1, vector<int>& nums2)
{
int n1 = nums1.size(), n2 = nums2.size();
vector<int> res;
if (n1 == 0 || n2 == 0)
return res;
unordered_map<int, int> dict;
for (int n : nums1)
dict
++;
for (int n : nums2)
if (dict.find(n) != dict.end() && --dict
>= 0)
res.push_back(n);
return res;
}
相关文章推荐
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2
- leetcode----Longest Substring Without Repeating Characters
- [LeetCode]47 Permutations II
- [LeetCode]65 Valid Number
- [LeetCode]123 Best Time to Buy and Sell Stock III
- [LeetCode] String Reorder Distance Apart
- [LeetCode] Sliding Window Maximum
- [LeetCode] Find the k-th Smallest Element in the Union of Two Sorted Arrays
- [LeetCode] Determine If Two Rectangles Overlap
- [LeetCode] A Distance Maximizing Problem
- leetcode_linearList
- leetcode_linearList02
- 021-Merge Two Sorted Lists(合并两个排好序的单链表);leetcode
- LeetCode[Day 1] Two Sum 题解
- LeetCode[Day 2] Median of Two Sorted Arrays 题解
- LeetCode[Day 3] Longest Substring Without... 题解
- LeetCode [Day 4] Add Two Numbers 题解