-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2215. Find the Difference of Two Arrays
66 lines (59 loc) · 1.46 KB
/
2215. Find the Difference of Two Arrays
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
58
59
60
61
62
63
64
65
66
class Solution {
public:
vector<vector<int>> findDifference(vector<int>& nums1, vector<int>& nums2) {
sort(nums1.begin(), nums1.end());
sort(nums2.begin(), nums2.end());
int n = nums1.size(), m = nums2.size();
int i=0, j=0;
set<int> n1, n2;
while(i<n || j<m)
{
if(i<n && j<m)
{
if(nums1[i]<nums2[j])
{
n1.insert(nums1[i]);
i++;
}
else if(nums1[i]>nums2[j])
{
n2.insert(nums2[j]);
j++;
}
else
{
int k = j;
while( j < m && nums1[i] == nums2[j])
{
j++;
}
while(i<n && nums1[i] == nums2[k])
{
i++;
}
}
}
else if(i<n)
{
n1.insert(nums1[i]);
i++;
}
else
{
n2.insert(nums2[j]);
j++;
}
}
vector<int> a;
vector<int> b;
for(auto &val : n1)
{
a.push_back(val);
}
for(auto &val : n2)
{
b.push_back(val);
}
return {a,b};
}
};