-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetcode-349.cpp
49 lines (44 loc) · 1.08 KB
/
leetcode-349.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <algorithm>
#include <vector>
#include <unordered_set>
using namespace std;
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
unordered_set<int> us;
vector<int> vec;
//if(!nums1.empty()||!nums2.empty())
// return vec;
int flag=0;
int n=nums1.size();
int m=nums2.size();
//cout<<n<<m<<endl;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(nums1[i]==nums2[j])
{
us.insert(nums1[i]);
flag++;
}
}
}
//cout<<flag<<endl;
//for(auto iter=us.begin();iter!=us.end();iter++)
//{
//cout<<*iter<<endl;
//}
copy(us.begin(),us.end(),back_inserter(vec));
return vec;
}
int main()
{
vector<int> vec1={1,2,2,1};
vector<int> vec2={2,2};
vector<int> vec3=intersection(vec1,vec2);
for(auto iter=vec3.begin();iter!=vec3.end();iter++)
{
cout<<*iter<<endl;
}
return 0;
}