LeetCode 350. Intersection of Two Arrays II
来源:互联网 发布:美术生送什么礼物 知乎 编辑:程序博客网 时间:2024/06/08 01:00
350. 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.
分析:将nums1的每一个数字以及对应数字的个数存储在map里面,遍历nums2中的所有元素,如果当前元素在map中存在,就把它放入result数组中,并将其数量-1。最终返回result即为所求交集~
class Solution {public: vector<int> 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++) { if(m[nums2[i]] != 0) { m[nums2[i]]--; result.push_back(nums2[i]); } } return result; }};
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
- 学习伪代码
- CF-Codeforces Round #392 (Div. 2)-A-Holiday Of Equality
- 哪个组件调用了fetchTopic函数
- 深挖SpringMVC_04_POST方法中文乱码解决方案
- VS常用快捷键大全
- LeetCode 350. Intersection of Two Arrays II
- 读《架构即未来》
- 蓝桥杯历届试题 打印十字图
- CF-Codeforces Round #392 (Div. 2)-B-Blown Garland
- 2.Rails程序框架
- 不定型极限的计算问题
- py2.7 : 《机器学习实战》 Logistic回归 1.22号 5.3 分析数据:随机梯度上升
- POJ1698 Alice's Chance 最大流Dinic
- pytorch 学习笔记(一)