leetcode 350. Intersection of Two Arrays II 解题小结
来源:互联网 发布:站长seo查询 编辑:程序博客网 时间:2024/06/08 07:29
题目是这样子的:
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.
大意是,求出两个数组的交集,集合是无序的且不限重
这道题比较简单,不过关键是后期时间上的优化,一开始自己没往哈希方面想,直接来个暴力的两个for循环嵌套解法,复杂度O(n^2)可是击败了2%的人,无限汗颜中w(゚Д゚)w,看了下dicuss发现有几个时间优化了挺多的解法。
第一种是先排序好两个数组,再利用迭代器进行遍历比较
class Solution {public: vector<int> intersect(vector<int>& nums1, vector<int>& nums2) { sort(nums1.begin(),nums1.end()); sort(nums2.begin(),nums2.end()); vector<int> a; vector<int>::iterator s1 = nums1.begin(),s2= nums2.begin(); while(s1!=nums1.end()&&s2!=nums2.end()) { while(s1!=nums1.end()&&s2!=nums2.end()&&*s1 == *s2) { a.push_back(*s1); s1++; s2++; } while(s2!=nums2.end()&&*s1>*s2) { s2++; } while(s1!=nums1.end()&&*s2>*s1) { s1++; } } return a; }};
第二种是利用了哈希:
a.没有用map,取两个数组的最大值作为长度开多一个数组进行哈希
class Solution {public: vector<int> intersect(vector<int>& nums1, vector<int>& nums2) { if(nums1.size()==0) return nums1; if(nums2.size()==0) return nums2; sort(nums1.begin(),nums1.end()); sort(nums2.begin(),nums2.end()); vector<int> b; int max1 = max(nums1[nums1.size()-1],nums2[nums2.size()-1]); vector<int> a(max1+1,0); for(int i = 0; i<nums1.size();i++) { a[nums1[i]]++; } for(int i = 0 ;i<nums2.size();i++) { if(a[nums2[i]]!=0) { b.push_back(nums2[i]); a[nums2[i]]--; } } return b; }};
b.利用了map进行哈希
class Solution {public: vector<int> intersect(vector<int>& nums1, vector<int>& nums2) { unordered_map<int,int> a; vector<int> b; for(int i = 0; i<nums1.size();i++) { a[nums1[i]]++; } for(int j = 0; j<nums2.size(); j++) { if(--a[nums2[j]]>=0) { b.push_back(nums2[j]); } } return b; }};
c.与b大致相同,但是在if()中加入了预判断键值是否存在,减少了空间复杂度
class Solution {public: vector<int> intersect(vector<int>& nums1, vector<int>& nums2) { unordered_map<int,int> a; vector<int> b; for(int i = 0; i<nums1.size();i++) { a[nums1[i]]++; } for(int j = 0; j<nums2.size(); j++) { if(a.find(nums2[j])!=a.end()&& --a[nums2[j]]>=0) { b.push_back(nums2[j]); } } return b; }};
0 0
- leetcode 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
- leetcode.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
- Leetcode 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
- [Leetcode] 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
- LeetCode 350. Intersection of Two Arrays II
- leetcode 350. Intersection of Two Arrays II
- 图片的异步加载和双缓存学习笔记——ImageLoader
- 开张语
- ActionBarSherlock学习笔记——初始化控件
- ActionBarSherlock学习笔记——添加组件Menu
- ListView优化
- leetcode 350. Intersection of Two Arrays II 解题小结
- ActionBarSherlock学习笔记——ActionBar添加View
- 学生学籍管理系统_管理员登陆对学生的信息进行操作
- ActionBarSherlock学习笔记——SupportActionBar()属性设置
- ActionBarSherlock学习笔记——requestWindowFeature
- jzoj 1359. 【2011.12普及模拟】数三角
- ActionBarSherlock学习笔记——SearchView
- 【Leetcode】122. Best Time to Buy and Sell Stock II 【动态规划&贪心】
- tomcat安装步骤以及启动一段时间自动停止