-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path350_intersectionOfTwoArr.cpp
More file actions
43 lines (41 loc) · 936 Bytes
/
Copy path350_intersectionOfTwoArr.cpp
File metadata and controls
43 lines (41 loc) · 936 Bytes
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
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;
class Solution {
public:
vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
map<int, int> map1;
for (auto num:nums1)
{
if (map1.find(num)!=map1.end() )
++map1[num];
else
map1[num] = 1;
}
map<int, int> map2;
for (auto num:nums2)
{
if (map2.find(num)!=map2.end())
++map2[num];
else
map2[num]=1;
}
vector<int> res;
for (auto& item:map1)
{
if (map2.find(item.first)!=map2.end())
{
for (size_t i=0; i<min(item.second, map2[item.first]); ++i)
res.push_back(item.first);
}
}
return res;
}
};
int main()
{
Solution solution;
return 0;
}