-
Notifications
You must be signed in to change notification settings - Fork 0
/
2215_Find-the-Difference-of-Two-Arrays.cpp
68 lines (56 loc) · 1.75 KB
/
2215_Find-the-Difference-of-Two-Arrays.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
class Solution {
public:
vector<vector<int>> findDifference(vector<int>& nums1, vector<int>& nums2) {
vector<vector<int>> arr(2, vector<int>());
int offset = 1000;
int hashmap[2001] = {0};
for(int idx = 0; idx < nums1.size(); idx++){
if(hashmap[nums1[idx]+offset] == 2) {
hashmap[nums1[idx]+offset] = -1;
}else if(hashmap[nums1[idx]+offset] == 0) {
hashmap[nums1[idx]+offset] = 1;
}
}
for(int idx = 0; idx < nums2.size(); idx++){
if(hashmap[nums2[idx]+offset] == 1) {
hashmap[nums2[idx]+offset] = -1;
}else if (hashmap[nums2[idx]+offset] == 0){
hashmap[nums2[idx]+offset] = 2;
}
}
for(int idx = 0; idx <= 2*offset; idx++){
if(hashmap[idx] == 1) {
arr[0].push_back(idx-offset);
}
if(hashmap[idx] == 2) {
arr[1].push_back(idx-offset);
}
}
return arr;
}
};
#define INPUT_MAX 1000
class Solution {
public:
vector<vector<int>> findDifference(vector<int>& nums1, vector<int>& nums2) {
vector<vector<int>> output(2, vector<int>());
int hash[2*INPUT_MAX+1] = {0};
int offset = 1000;
// bucket sort
for(int &ele : nums1){
hash[ele+offset] |= 1; //bit0
}
for(int &ele:nums2){
hash[ele+offset] |= 2; //bit1
}
for(int i = 0; i < (2*INPUT_MAX+1); ++i){
if(hash[i] == 1){
output[0].push_back(i-offset);
}
if(hash[i] == 2){
output[1].push_back(i-offset);
}
}
return output;
}
};