91. Intersection of Two Arrays
来源:互联网 发布:怎么申请多个淘宝号 编辑:程序博客网 时间:2024/04/29 15:02
-350. Intersection of Two Arrays II My Submissions QuestionEditorial Solution
Total Accepted: 5040 Total Submissions: 12104 Difficulty: Easy
Given two arrays, write a function to compute their intersection.
Example:
Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2].
数组求交集,元素不唯一
和上一题不同,这一题,统计相同元素的个数
push的时候做参考
class Solution {public: vector<int> intersect(vector<int>& nums1, vector<int>& nums2) { int n1=nums1.size(),n2=nums2.size(); unordered_map<int,int> minset; //较小容器集合,结果集合 vector<int> res; //保存结果 auto it_beg = n1<n2?nums1.begin():nums2.begin(); auto it_end = n1<n2?nums1.end():nums2.end(); while(it_beg!=it_end){ //将较小容器元素集合保存,并统计个数 if(minset.count(*it_beg))minset[*it_beg]++; else minset[*it_beg]=1; ++it_beg; } it_beg = n1>=n2?nums1.begin():nums2.begin(); it_end = n1>=n2?nums1.end():nums2.end(); while(it_beg!=it_end){ //扫描较大容器集合,如果是二者交集存入结果集 if(minset.count(*it_beg)){ if(minset[*it_beg]>0)res.push_back(*it_beg); //push的个数足够了就不再push --minset[*it_beg]; }; ++it_beg; } return res; }};
0 0
- 91. Intersection of Two Arrays
- Intersection of Two Arrays
- Intersection of Two Arrays
- Intersection of Two Arrays
- Intersection of Two Arrays
- Intersection of Two Arrays
- Intersection of Two Arrays
- Intersection of Two Arrays
- Intersection of Two Arrays
- Intersection of Two Arrays
- Intersection of Two Arrays
- Intersection of Two Arrays
- Intersection of Two Arrays
- Intersection of Two Arrays
- Intersection of two arrays
- Intersection of Two Arrays
- Intersection of Two Arrays
- Intersection of Two Arrays
- kobject
- 计蒜客之矩阵翻转
- 网页性能优化
- 代码训练营——进制转换
- 计数方式比较
- 91. Intersection of Two Arrays
- 154_ListView加入两种条目
- 从零开始学_JavaScript_系列(21)——dojo(8)(手把手教你封装一个widget)
- SVM笔记(五) 非线性支持向量机
- leetcode 349. Intersection of Two Arrays
- JavaScript中的this
- 20150401数据类型补充
- 155_ViewDragHelper的使用
- c语言学习笔记之数组小结