forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
47 lines (34 loc) · 1.09 KB
/
main.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
/// Source : https://leetcode.com/problems/intersection-of-two-arrays/description/
/// Author : liuyubobobo
/// Time : 2017-07-12
#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;
/// Hash Set
/// Time complexity: O(len(nums1) + len(nums2))
/// Space Complexity: O(len(nums1))
class Solution {
public:
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
unordered_set<int> record(nums1.begin(), nums1.end());
unordered_set<int> resultSet;
for( int i = 0 ; i < nums2.size() ; i ++ )
if( record.find(nums2[i]) != record.end() )
resultSet.insert( nums2[i] );
return vector<int>(resultSet.begin(), resultSet.end());
}
};
void printVec(const vector<int>& vec){
for(int e: vec)
cout << e << " ";
cout << endl;
}
int main() {
int nums1[] = {1, 2, 2, 1};
vector<int> vec1(nums1, nums1 + sizeof(nums1)/sizeof(int));
int nums2[] = {2, 2};
vector<int> vec2(nums2, nums2 + sizeof(nums2)/sizeof(int));
printVec(Solution().intersection(vec1, vec2));
return 0;
}