-
Notifications
You must be signed in to change notification settings - Fork 0
/
find-the-difference-of-two-arrays.cpp
119 lines (101 loc) · 2.55 KB
/
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// https://leetcode.com/problems/find-the-difference-of-two-arrays/
// 1. upgraded map version
class Solution {
public:
// return elements only appear in n1
vector<int> helper(vector<int> &n1, vector<int> &n2) {
unordered_map<int, int> mp;
// put all element in n1 to the map
for (auto i : n1) {
mp[i]++;
}
// erase the repeat elements both in n1 & n2
for (auto i : n2) {
auto it = mp.find(i);
if (it != mp.end())
mp.erase(it);
}
// return the distinct elements
vector<int> ans;
for (auto i : mp) {
ans.push_back(i.first);
}
return ans;
}
vector<vector<int>> findDifference(vector<int> &nums1, vector<int> &nums2) {
vector<vector<int>> ans;
ans.push_back(helper(nums1, nums2));
ans.push_back(helper(nums2, nums1));
return ans;
}
};
// 2. plain map version
class Solution {
public:
vector<vector<int>> findDifference(vector<int> &nums1, vector<int> &nums2) {
map<int, int> mp1;
map<int, int> mp2;
for (auto i : nums1) {
mp1[i]++;
}
for (auto i : nums2) {
mp2[i]++;
}
vector<int> v1;
for (auto i : mp1) {
int key = i.first;
// find the distinct element
if (mp2.find(key) == mp2.end())
v1.push_back(key);
}
vector<int> v2;
for (auto j : mp2) {
int key = j.first;
// find the distinct element
if (mp1.find(key) == mp1.end())
v2.push_back(key);
}
vector<vector<int>> ans;
ans.push_back(v1);
ans.push_back(v2);
return ans;
}
};
// ugly one by loop
class Solution {
public:
vector<vector<int>> findDifference(vector<int> &nums1, vector<int> &nums2) {
vector<vector<int>> ans;
vector<int> new1;
vector<int> new2;
for (int i = 0; i < nums1.size(); i++) {
bool exists = std::find(std::begin(new1), std::end(new1), nums1[i]) !=
std::end(new1);
if (exists == true)
continue;
int f = 0;
for (int j = 0; j < nums2.size(); j++) {
if (nums1[i] == nums2[j])
f = 1;
}
if (f != 1)
new1.push_back(nums1[i]);
}
for (int i = 0; i < nums2.size(); i++) {
bool exists = std::find(std::begin(new2), std::end(new2), nums2[i]) !=
std::end(new2);
if (exists == true)
continue;
int f = 0;
for (int j = 0; j < nums1.size(); j++) {
if (nums2[i] == nums1[j])
f = 1;
}
if (f != 1)
new2.push_back(nums2[i]);
}
ans.push_back(new1);
ans.push_back(new2);
return ans;
}
};