Intersection of Two Arrays II LeetCode solution in C++

1
class Solution {
2
public:
3
vector<int> intersect(vector<int> &nums1, vector<int> &nums2) {
4
sort(nums1.begin(), nums1.end());
5
sort(nums2.begin(), nums2.end());
6

7
int a = nums1.size();
8
int b = nums2.size();
9

10
int i = 0, j = 0;
11
vector<int> res;
12

13
while (i < a && j < b) {
14
if (nums1[i] > nums2[j]) {
15
j++;
16
} else if (nums1[i] < nums2[j]) {
17
i++;
18
} else {
19
res.push_back(nums1[i]);
20
i++;
21
j++;
22
}
23
}
24

25
return res;
26
}
27
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0