350. Intersection of Two Arrays II
来源:互联网 发布:python 变量生存期 编辑:程序博客网 时间:2024/05/18 01:04
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?
解答: 题目很容易理解,就是在两个数组中找相同的元素。这里有很多的解法,我都会粘贴上来。
值得高兴的是,我的方法用时8ms, 击败86%,哈哈~ 不过貌似简单的方法也能达到如此。
1.用二分查找,这里受 278. First Bad Version 的启发,将二分查找修改使得适用于能返回重复元素找到第一个位置。
同时符合follow up中第三条,对每个nums2中的元素遍历获取,然后从nums1中去查找有无相同元素,同时用一个标记数组标记num1,使得若找到,则判断该位置上是否已经被获取过,若被获取过,则到下一个位置判断有没有获取过,直到下一个位置没被获取过(则加入结果集合中)或者与想要的元素值不同(跳过)。
class Solution {public: int searchfirst(int num, vector<int>& vec){ int begin = 0; int end = vec.size()-1; int middle; while(begin < end - 1){ middle = begin+(end-begin)/2; if(vec[middle] >= num){ end = middle; }else begin = middle; } if(vec[begin] == num) return begin; else return end;}vector<int> intersect(vector<int>& nums1, vector<int>& nums2) { int len1 = nums1.size(); sort(nums1.begin(), nums1.end()); vector<bool>v1(len1, false); vector<int>res; int len2 = nums2.size(); if(len1 == 0 || len2 == 0) return res; int i = 0; while(i < len2){ int temp = nums2[i]; int index = searchfirst(temp,nums1); while(nums1[index] == temp){ if(v1[index] == false){ res.push_back(temp); v1[index]=true; break; }else index++; } ++i; } return res;}};
下面粘贴其他的方法
2.对两个数组都进行排序,然后进行比较(这个基于两个数组都能一次性download下来)
Sort and two pointers Solution: Time: O(max(m, n) log(max(m, n))) Space: O(m + n)
vector<int> intersect(vector<int>& nums1, vector<int>& nums2) { vector<int> result; result.reserve(min(nums1.size(), nums2.size())); sort(nums1.begin(), nums1.end()); sort(nums2.begin(), nums2.end()); int i1 = 0, i2 = 0, len1 = nums1.size(), len2 = nums2.size(); while(i1 < len1 && i2 < len2) { if(nums1[i1] == nums2[i2]) { result.push_back(nums1[i1]); ++i1; ++i2; } else if(nums1[i1] < nums2[i2]) ++i1; else ++i2; } return result;}
3.使用hashmap
(1)m: nums1.size n: nums2.size
Hash table solution: Time: O(m + n) Space: 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; }};
(2)Hash table solution2: Time: O(m + n) Space: O(m)
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.find(nums2[i]) != dict.end() && --dict[nums2[i]] >= 0) res.push_back(nums2[i]); return res; }};
0 0
- 350. Intersection of Two Arrays II
- LeetCode 350. Intersection of Two Arrays II
- leetcode.350. Intersection of Two Arrays II
- 350. Intersection of Two Arrays II【E】
- 350. Intersection of Two Arrays II
- [leetcode] 350. Intersection of Two Arrays II
- Leetcode 350. Intersection of Two Arrays II
- leetcode 350. Intersection of Two Arrays II
- 350. Intersection of Two Arrays II
- 350. Intersection of Two Arrays II
- Leetcode 350. Intersection of Two Arrays II
- [leetcode] 350. Intersection of Two Arrays II
- leetcode 350. Intersection of Two Arrays II
- 350. Intersection of Two Arrays II
- 350. Intersection of Two Arrays II
- 【leetcode】350. Intersection of Two Arrays II
- 350. Intersection of Two Arrays II
- 350. Intersection of Two Arrays II
- Jquery +css弹出层的操作
- 使用STAR方法完善简历
- 解决Spring4整合Hibernate5的jar冲突问题
- Zend Studio如何打开不支持的文件格式?
- SQL Server 2008登录错误:无法连接到(local)的解决
- 350. Intersection of Two Arrays II
- Map排序
- 窗口按钮样式老旧解决方法
- 133. Clone Graph
- 转载http://lib.csdn.net/article/php/24076?knId=948
- 判断一个int数组中的元素是否存在重复
- shell编程和正则表达式相关知识
- ThreadLocal的使用
- Activity的启动模式