-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path350.cpp
More file actions
39 lines (35 loc) · 1018 Bytes
/
Copy path350.cpp
File metadata and controls
39 lines (35 loc) · 1018 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
class Solution {
public:
vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
unordered_map<int, int> dict;
vector<int> res;
for(int i = 0; i < (int)nums1.size(); i++) dict[nums1[i]]++;
for(int i = 0; i < (int)nums2.size(); i++)
if(--dict[nums2[i]] >= 0) res.push_back(nums2[i]);
return res;
}
};
class Solution {
public:
vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
sort(nums1.begin(), nums1.end());
sort(nums2.begin(), nums2.end());
int n1 = (int)nums1.size(), n2 = (int)nums2.size();
int i1 = 0, i2 = 0;
vector<int> res;
while(i1 < n1 && i2 < n2){
if(nums1[i1] == nums2[i2]) {
res.push_back(nums1[i1]);
i1++;
i2++;
}
else if(nums1[i1] > nums2[i2]){
i2++;
}
else{
i1++;
}
}
return res;
}
};