-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntersectionofTwoArrays.cpp
48 lines (41 loc) · 1.01 KB
/
IntersectionofTwoArrays.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
#include <iostream>
#include <vector>
#include <utility>
#include <unordered_map>
#include <algorithm>
using namespace std;
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
vector<int> res;
if (nums1.empty() || nums2.empty())
return res;
unordered_map<int, size_t> isu;
for (const auto e : nums1)
isu[e] = 1;
for (const auto e : nums2) {
if (isu.find(e) != isu.end()) {
res.push_back(e);
isu.erase(isu.find(e));
}
}
return res;
}
vector<int> intersection1(vector<int>& nums1, vector<int>& nums2) {
// Write your code here
sort(nums1.begin(), nums1.end());
sort(nums2.begin(), nums2.end());
vector<int> intersect;
vector<int>::iterator it1 = nums1.begin(), it2 = nums2.begin();
while ((it1 != nums1.end()) && (it2 != nums2.end())) {
if (*it1 < *it2) it1++;
else if (*it1 > *it2) it2++;
else {
intersect.push_back(*it1);
it1++; it2++;
}
}
auto last = unique(intersect.begin(), intersect.end());
intersect.erase(last, intersect.end());
return intersect;
}
int main() {
}