LeetCode : Intersection of Two Arrays II
来源:互联网 发布:约瑟夫环 java 数组 编辑:程序博客网 时间:2024/05/16 10:24
第一种方法是把数组放在map中进行比较。
第二种方法是将数组排好序后一次比较。
Solution 1
vector<int> Solution::intersect(vector<int>& nums1, vector<int>& nums2){ vector<int> result; map<int, int> m; for(int i = 0; i < nums1.size(); i++) m[nums1[i]] ++; for(int i = 0; i < nums2.size(); i++) { m[nums2[i]] --; if(m[nums2[i]] >= 0) result.push_back(nums2[i]); } return result;}
Solution 2
vector<int> Solution::intersect(vector<int>& nums1, vector<int>& nums2){ vector<int> result; sort(nums1.begin(), nums1.end()); sort(nums2.begin(), nums2.end()); for(int i = 0, j = 0; i < nums1.size() && j < nums2.size();) { if(nums1[i] == nums2[j]) { result.push_back(nums1[i]); i++; j++; } else if(nums1[i] > nums2[j]) j++; else i++; } return result;}
0 0
- [LeetCode] Intersection of Two Arrays II
- LeetCode Intersection of Two Arrays II
- LeetCode 350. Intersection of Two Arrays II
- LeetCode: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 Intersection of Two Arrays II
- Leetcode 350. Intersection of Two Arrays II
- [python][leetcode]Intersection of Two Arrays II
- [leetcode] 350. Intersection of Two Arrays II
- leetcode 350. Intersection of Two Arrays II
- 【leetcode】Intersection of Two Arrays II
- 【leetcode】350. Intersection of Two Arrays II
- leetcode之Intersection of Two Arrays II
- [Leetcode] 350. Intersection of Two Arrays II
- RecyclerView布局靠左问题
- URL转码
- Json数据解析
- [leetcode]401. Binary Watch
- Git创建仓库
- LeetCode : Intersection of Two Arrays II
- PorterDuffXferMode不正确的真正原因PorterDuffXferMode深入试验)
- 64位linux安装32位库
- Git基本操作
- 自定义eclipse启动画面
- CentOS7 搭建Git服务器
- java异常处理机制
- HDU 1079 Calendar Game(简单博弈)
- 学习服务器过程遇到的英文单词,mark