[leetcode]349.Intersection of Two Arrays
2016-05-21 18:18
465 查看
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.
Subscribe to see which companies asked this question
Solution:
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.
Subscribe to see which companies asked this question
Solution:
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) { unordered_set<int> htable; unordered_set<int> htmp; vector<int> ret; for (int i = 0; i < (int)nums1.size(); i++) htable.insert(nums1[i]); for (int i = 0; i < (int)nums2.size(); i++) if (htable.find(nums2[i]) != htable.end()) htmp.insert(nums2[i]); for (auto it = htmp.begin(); it != htmp.end(); ++it) ret.push_back(*it); return ret; }
相关文章推荐
- Android中Preference的使用以及监听事件分析(自己学习)
- windows下安装NetBeans IDE & 配置C++编译环境
- 回调函数
- POJ-5690-All X(快速幂/循环节)
- 链表的插入排序
- chromium启动流程和消息处理是怎么关联起来
- Linux下的gpio调试方法
- All X 百度之星初赛
- Installation error: INSTALL_FAILED_CONFLICTING_PROVIDER 解决方案-Android
- android 4.2上面实现录制系统内置声音remote submix
- HTML5新增元素(上)
- ubuntu安装vmware tool
- 【Asp.Net】——ADO.NET为访问数据插上了翅膀
- **代码审查:Phabricator命令行工具Arcanist的基本用法
- Bootstrap源码分析之dropdown
- 10006---Linux 命令别名永久化
- u盘安装redhat 7
- zabbix3.0.2 使用percona模版监控mysql从库的漏洞修复
- 课程设计分工
- 【数论】noip2009Hankson 的趣味题