LeetCode-349&350.Intersection of Two Arrays
来源:互联网 发布:淘宝图片拍摄技巧 编辑:程序博客网 时间:2024/05/18 01:54
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:
- 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[n]++; for (int n : nums2) if (dict.find(n) != dict.end() && --dict[n] >= 0) res.push_back(n); return res; }
- LeetCode-349&350.Intersection of Two Arrays
- Leetcode-349 Intersection of Two Arrays
- leetcode 349 Intersection of Two Arrays C++
- LeetCode 349 Intersection of Two Arrays
- LeetCode-349 Intersection of Two Arrays
- leetcode-Intersection of Two Arrays-349
- LeetCode 349 Intersection of Two Arrays
- LeetCode#349 Intersection of Two Arrays
- LeetCode(349)Intersection of Two Arrays
- [LeetCode]349 Intersection of Two Arrays
- [LeetCode-349] Intersection of Two Arrays(java)
- Leetcode 349 Intersection of Two Arrays
- LeetCode No.349 Intersection of Two Arrays
- leetcode-349-Intersection of Two Arrays
- [Leetcode 349]Intersection of Two Arrays
- leetcode 349 Intersection of Two Arrays
- Leetcode #349 Intersection of Two Arrays
- LeetCode 349 Intersection of Two Arrays
- Spring MVC 获取请求参数的几种方法
- cocos2dx之onEnter,onExit
- [leetcode] 【字符串】 67. Add Binary
- 面向对象多继承
- 模板类错误程序
- LeetCode-349&350.Intersection of Two Arrays
- Mybatis学习与Spring MVC 的集成(6)
- 高并发的简单处理方法
- tornado study 1
- HttpURLConnection请求数据
- 从线行回归到高斯过程分类
- 单链表面试题系列之链表相交问题
- Java学习之集合框架(中)
- rpc