-
Notifications
You must be signed in to change notification settings - Fork 0
/
intersection_349.cpp
46 lines (41 loc) · 1.04 KB
/
intersection_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
#include<unordered_set>
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
unordered_set<int> set1, set2;
for (auto num : nums1) {
set1.insert(num);
}
for (auto num : nums2) {
set2.insert(num);
}
return getIntersection(set1, set2);
}
vector<int> getIntersection(unordered_set<int>& set1, unordered_set<int>& set2) {
if (set1.size() > set2.size()) {
return getIntersection(set2, set1);
}
vector<int> intersection;
for (auto num : set1) {
if (set2.count(num)) {
intersection.push_back(num);
}
}
return intersection;
}
};
int main(){
vector<int> nums1;
vector<int> nums2;
nums1.push_back(1);
nums1.push_back(2);
nums1.push_back(2);
nums1.push_back(1);
nums2.push_back(2);
nums2.push_back(2);
Solution sol;
sol.intersection(nums1, nums2);
}