-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintersection_of_two_arrays.cpp
More file actions
57 lines (54 loc) · 1.4 KB
/
intersection_of_two_arrays.cpp
File metadata and controls
57 lines (54 loc) · 1.4 KB
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
51
52
53
54
55
56
57
/*
* =====================================================================================
*
* Filename: intersection_of_two_arrays.cpp
*
* Description: Intersection of Two Arrays.
*
* Version: 1.0
* Created: 03/04/19 11:44:17
* Revision: none
* Compiler: gcc
*
* Author: Zhu Xianfeng (), xianfeng.zhu@gmail.com
* Organization:
*
* =====================================================================================
*/
#include <stdio.h>
#include <vector>
#include <unordered_set>
class Solution
{
public:
std::vector<int> intersection(const std::vector<int>& nums1, const std::vector<int>& nums2)
{
std::unordered_set<int> unordered_nums(nums1.begin(), nums1.end());
std::vector<int> mixed_nums;
for (auto num: nums2)
{
if (unordered_nums.count(num) > 0)
{
mixed_nums.push_back(num);
unordered_nums.erase(num);
}
}
return mixed_nums;
}
};
int main(int argc, char* argv[])
{
std::vector<int> nums1 = {1, 2, 3, 4, 5};
std::vector<int> nums2 = {3, 4, 5, 6, 7};
auto mixed_nums = Solution().intersection(nums1, nums2);
if (mixed_nums.size() > 0)
{
printf("Intersection:");
for (auto num: mixed_nums)
{
printf(" %d", num);
}
printf("\n");
}
return 0;
}