LeetCode 349. Intersection of Two Arrays
来源:互联网 发布:js删除数组里面空格 编辑:程序博客网 时间:2024/06/05 19:06
题意
求集合nums1和nums2的交集
题解
使用C++ STL的unordered_set来存储nums1,对nums2的每一个元素,判断nums1中是否存在,存在就放入返回结果集res中,并从nums1删除该元素。
class Solution {public: vector<int> intersection(vector<int>& nums1, vector<int>& nums2) { unordered_set<int> numset(nums1.begin(), nums1.end()); vector<int> res; for (auto i : nums2) //m中对i计数,有一个就放入res,并从numset中删除 if (numset.count(i)) { //数组最后添加a res.push_back(i); //m中删除元素a numset.erase(i); } return res; }};
阅读全文
0 0
- leetcode 记录 349. Intersection of Two Arrays
- 349.[Leetcode]Intersection of Two Arrays
- [leetcode] 349. Intersection of Two Arrays
- Leetcode Everyday: 349. Intersection of Two Arrays
- leetcode.349. Intersection of Two Arrays
- LeetCode 349. Intersection of Two Arrays
- Leetcode 349. Intersection of Two Arrays
- leetcode 349. Intersection of Two Arrays
- leetcode 349. Intersection of Two Arrays
- LeetCode 349. Intersection of Two Arrays
- 【leetcode】 349. Intersection of Two Arrays
- Leetcode 349. Intersection of Two Arrays
- LeetCode 349. Intersection of Two Arrays
- Leetcode题解 349. Intersection of Two Arrays
- LeetCode - 349. Intersection of Two Arrays
- [Leetcode]349. Intersection of Two Arrays
- 349. Intersection of Two Arrays--LeetCode Record
- LeetCode 349. Intersection of Two Arrays
- linux 时间戳
- JSON格式与string格式互相转换
- Guice入门实例和单例模式
- Java线程的sleep和interrupt方法测试
- Kinect V1结构光激光散斑原理及样式
- LeetCode 349. Intersection of Two Arrays
- 强大的Android控件之RecyclerView体验艺术般的控件
- 文件的原子操作
- 浏览器兼容获取XmlHttpRequest对象
- STM32学习笔记一一外部中断
- java的阻塞队列blockingqueue
- chapter2-有向图
- python正则表达式对照表
- TinyXML:一个优秀的C++ XML解析器