leetcode之349. Intersection of Two Arrays (C++解法 哈希表和vector)
2016-09-14 14:12
465 查看
原题:
Given two arrays, write a function to compute their intersection.
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.
[b]***************************[/b]我是一条分割线[b]********************************[/b]
Given two arrays, write a function to compute their intersection.
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.
[b]***************************[/b]我是一条分割线[b]********************************[/b]
class Solution { public: vector<int> intersection(vector<int>& nums1, vector<int>& nums2) { map<int ,int > couns1; map<int ,int > couns2; for(int i=0;i<nums1.size();i++) { couns1.insert({nums1[i],i}); } vector<int> resu; for(int i=0;i<nums2.size();i++) { if(couns1.find(nums2[i])==couns1.end()) continue; else { if(resu.end()==find(resu.begin(),resu.end(),nums2[i])) resu.push_back(nums2[i]); else continue; } } return resu; } }; //附测试代码 int main() { int array1[]={1,2,3,4,5,4,3,2,1}; int array2[]={1,2,3,4,5,4,3,2,1}; Solution c; vector<int > ve1(array1,array1+9); vector<int > ve2(array2,array2+9); vector<int > resu; resu=c.intersection(ve1,ve2); for(int i=0;i<resu.size();i++) cout<<resu[i]<<endl; return 0; }
相关文章推荐
- Windows Powershell使用哈希表
- 探索PowerShell (八) 数组、哈希表(附:复制粘贴技巧)
- 轻松学习C#的哈希表
- C#中哈希表(HashTable)用法实例详解(添加/移除/判断/遍历/排序等)
- PHP内核探索:哈希表碰撞攻击原理
- php内核解析:PHP中的哈希表
- java中哈希表及其应用详解
- C#中HashTable的用法
- 哈希函数
- 哈希表及哈希函数研究综述
- Qt中QSet的使用
- 数据结构-散列表(Hash Table)的C++实现模板
- 数据结构——哈希表
- Two Sum,3Sum,3Sum Closest,4Sum
- 哈希表
- Java-Majority Element
- 哈希表及处理冲突的方法
- 散列表/哈希表的基础知识(上)
- 常用算法C#实现:字符串包含问题
- 【Redis基本数据结构】字典实现