LeetCode349. Intersection of Two Arrays
来源:互联网 发布:帝国时代罗马复兴mac 编辑:程序博客网 时间:2024/06/14 10:56
Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2].
寻找两个数组的交叉元素,并且不能重复。
不能重复的话,首先想到set。先把一个数组的元素放进set,此时set里面的元素是去重的,然后遍历另外一个数组,看是否在set里面,如果在,则将这个这个元素放进set。最后,把set里面的元素放进vector返回。
class Solution {public: vector<int> intersection(vector<int>& nums1, vector<int>& nums2) { set<int> n1,n2,re1; vector<int> res; set<int>::iterator it; for (int i=0;i<nums1.size();i++) n1.insert(nums1[i]); for (int i=0;i<nums2.size();i++) { it=n1.find(nums2[i]); if (it != n1.end()) re1.insert(*it); } it = re1.begin(); while(it != re1.end()) { res.push_back(*it); it++; } return res; }};
0 0
- leetcode349: Intersection of Two Arrays
- Leetcode349. Intersection of Two Arrays
- Leetcode349. Intersection of Two Arrays
- Leetcode349. Intersection of Two Arrays
- LeetCode349. Intersection of Two Arrays
- LeetCode349 Intersection of Two Arrays
- leetcode349~Intersection of Two Arrays
- leetcode349. Intersection of Two Arrays
- leetcode349. Intersection of Two Arrays
- Leetcode349 - Intersection of Two Arrays(two pointers)
- LeetCode349. 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
- Java中的内部类总结
- Organopolysilazane (room temperature curing coating resin) IOTA-OPSZ-9150
- 某软件公司的GUID生成代码(GUIDFactory)java+C#
- 01 走进Spring,Context、Bean和IoC
- iOS时间问题
- LeetCode349. Intersection of Two Arrays
- (PHPSInorganic Polysilazane IOTA-PHPS
- m3u8视频格式的支持
- 程序员英语
- C/C++——虚函数
- struts2开发流程
- 03 Spring IoC之对象间的相互关系和 Spring 应用结构
- 指针获取增强屏幕上的字段值
- tomcat服务器部署