leetcode.349. Intersection of Two Arrays
2016-05-20 15:59
441 查看
Given two arrays, write a function to compute their intersection.
Example:
Given nums1 =
Note:
Each element in the result must be unique.
The result can be in any order.
Example:
Given nums1 =
[1, 2, 2, 1], nums2 =
[2, 2], return
[2].
Note:
Each element in the result must be unique.
The result can be in any order.
class Solution { public: vector<int> intersection(vector<int>& nums1, vector<int>& nums2) { set<int>s1,s2; int n1=nums1.size(),n2=nums2.size(); for(int i=0;i<n1;i++) s1.insert(nums1[i]); for(int i=0;i<n2;i++) s2.insert(nums2[i]); vector<int>res; set<int>::iterator it1=s1.begin(),it2=s2.begin(); while(it1!=s1.end()&&it2!=s2.end()) { if(*it1==*it2) { res.push_back(*it1); it1++; it2++; } else if(*it1<*it2) it1++; else it2++; } return res; } };
相关文章推荐
- LeetCode 45 Jump Game II
- In House打包流程
- ObjectInputStream序列化反序列化
- VC枚举系统所有字体
- 制作自己的镜像(一)
- 给dnf添加代理
- 六十二 Web开发 使用模板
- [转]使用Enumeration和Iterator遍历集合类
- localhost不能访问127.0.0.1可以访问的原因及解决方法 被打磨的不像人样
- modelsim编译vivado ip报错error protect region
- <根据马士兵>动态加载过程
- 论ORACLE 9i 的体系图为什么这么金典
- 文件与IO(一)之字节流
- mysql新建用户表,赋权,远程登陆
- hdu4707 Pet
- nginx学习笔记六(Nginx事件框架处理流程)
- 菜鸟从零开始初始WebSocket -Server篇(二)
- php抓取网页上的指定内容
- Android深入浅出自定义控件(一)
- spring-boot学习笔记