-
Notifications
You must be signed in to change notification settings - Fork 0
/
201019-1.cpp
50 lines (47 loc) · 920 Bytes
/
201019-1.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
50
// https://leetcode-cn.com/problems/intersection-of-two-arrays/
#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;
class Solution {
public:
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
unordered_set<int> m;
for (auto e : nums1) {
m.insert(e);
}
unordered_set<int> m2;
for (auto e : nums2) {
m2.insert(e);
}
vector<int> r;
for (auto e : m2) {
if (m.find(e) != m.end()) {
r.push_back(e);
}
}
return r;
}
};
void print(const vector<int>& a)
{
for (auto e : a) cout << e << " ";
cout << endl;
}
int main()
{
Solution s;
{
vector<int> nums1 = {1,2,2,1};
vector<int> nums2 = {2,2};
vector<int> r = s.intersection(nums1, nums2);
print(r); // answer: [2]
}
{
vector<int> nums1 = {4,9,5};
vector<int> nums2 = {9,4,9,8,4};
vector<int> r = s.intersection(nums1, nums2);
print(r); // answer: [9,4]
}
return 0;
}