@@ -45,7 +45,11 @@ Every integer in nums2 is present in nums1. Therefore, answer[1] = [].
45
45
46
46
## Solutions
47
47
48
- ### Solution 1
48
+ ### Solution 1: Hash Table
49
+
50
+ We define two hash tables $s1$ and $s2$ to store the elements in arrays $nums1$ and $nums2$ respectively. Then we traverse each element in $s1$. If this element is not in $s2$, we add it to the first list in the answer. Similarly, we traverse each element in $s2$. If this element is not in $s1$, we add it to the second list in the answer.
51
+
52
+ The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the length of the array.
49
53
50
54
<!-- tabs:start -->
51
55
@@ -61,8 +65,6 @@ class Solution {
61
65
public List<List<Integer > > findDifference (int [] nums1 , int [] nums2 ) {
62
66
Set<Integer > s1 = convert(nums1);
63
67
Set<Integer > s2 = convert(nums2);
64
-
65
- List<List<Integer > > ans = new ArrayList<> ();
66
68
List<Integer > l1 = new ArrayList<> ();
67
69
List<Integer > l2 = new ArrayList<> ();
68
70
for (int v : s1) {
@@ -75,9 +77,7 @@ class Solution {
75
77
l2. add(v);
76
78
}
77
79
}
78
- ans. add(l1);
79
- ans. add(l2);
80
- return ans;
80
+ return List . of(l1, l2);
81
81
}
82
82
83
83
private Set<Integer > convert (int [] nums ) {
@@ -97,12 +97,16 @@ public:
97
97
unordered_set<int > s1(nums1.begin(), nums1.end());
98
98
unordered_set<int > s2(nums2.begin(), nums2.end());
99
99
vector<vector<int >> ans(2);
100
- for (int v : s1)
101
- if (!s2.count (v))
100
+ for (int v : s1) {
101
+ if (!s2.contains (v)) {
102
102
ans[ 0] .push_back(v);
103
- for (int v : s2)
104
- if (!s1.count(v))
103
+ }
104
+ }
105
+ for (int v : s2) {
106
+ if (!s1.contains(v)) {
105
107
ans[ 1] .push_back(v);
108
+ }
109
+ }
106
110
return ans;
107
111
}
108
112
};
@@ -134,10 +138,11 @@ func findDifference(nums1 []int, nums2 []int) [][]int {
134
138
135
139
``` ts
136
140
function findDifference(nums1 : number [], nums2 : number []): number [][] {
137
- return [
138
- [... new Set <number >(nums1 .filter (v => ! nums2 .includes (v )))],
139
- [... new Set <number >(nums2 .filter (v => ! nums1 .includes (v )))],
140
- ];
141
+ const s1: Set <number > = new Set (nums1 );
142
+ const s2: Set <number > = new Set (nums2 );
143
+ nums1 .forEach (num => s2 .delete (num ));
144
+ nums2 .forEach (num => s1 .delete (num ));
145
+ return [Array .from (s1 ), Array .from (s2 )];
141
146
}
142
147
```
143
148
@@ -170,15 +175,11 @@ impl Solution {
170
175
* @return {number[][]}
171
176
*/
172
177
var findDifference = function (nums1 , nums2 ) {
173
- let ans1 = new Set (nums1),
174
- ans2 = new Set (nums2);
175
- for (let num of nums1) {
176
- ans2 .delete (num);
177
- }
178
- for (let num of nums2) {
179
- ans1 .delete (num);
180
- }
181
- return [Array .from (ans1), Array .from (ans2)];
178
+ const s1 = new Set (nums1);
179
+ const s2 = new Set (nums2);
180
+ nums1 .forEach (num => s2 .delete (num));
181
+ nums2 .forEach (num => s1 .delete (num));
182
+ return [Array .from (s1), Array .from (s2)];
182
183
};
183
184
```
184
185
@@ -190,59 +191,13 @@ class Solution {
190
191
* @return Integer[][]
191
192
*/
192
193
function findDifference($nums1, $nums2) {
193
- $rs = [[], []];
194
- $hashtable1 = array_flip(array_unique($nums1));
195
- $hashtable2 = array_flip(array_unique($nums2));
196
- for ($m = 0; $m < count($nums1); $m++) {
197
- if (!isset($hashtable2[$nums1[$m]])) {
198
- $rs[0][$m] = $nums1[$m];
199
- $hashtable2[$nums1[$m]] = 1;
200
- }
201
- }
202
- for ($n = 0; $n < count($nums2); $n++) {
203
- if (!isset($hashtable1[$nums2[$n]])) {
204
- $rs[1][$n] = $nums2[$n];
205
- $hashtable1[$nums2[$n]] = 1;
206
- }
207
- }
208
- return $rs;
209
- }
210
- }
211
- ```
194
+ $s1 = array_flip($nums1);
195
+ $s2 = array_flip($nums2);
212
196
213
- <!-- tabs: end -->
214
-
215
- ### Solution 2
216
-
217
- <!-- tabs: start -->
197
+ $diff1 = array_diff_key($s1, $s2);
198
+ $diff2 = array_diff_key($s2, $s1);
218
199
219
- ``` rust
220
- impl Solution {
221
- pub fn find_difference (nums1 : Vec <i32 >, nums2 : Vec <i32 >) -> Vec <Vec <i32 >> {
222
- const N : usize = 2001 ;
223
- let to_index = | i | (i as usize ) + 1000 ;
224
-
225
- let mut is_in_nums1 = [false ; N ];
226
- let mut is_in_nums2 = [false ; N ];
227
- let mut res1 = vec! [];
228
- let mut res2 = vec! [];
229
- for & num in nums1 . iter () {
230
- is_in_nums1 [to_index (num )] = true ;
231
- }
232
- for & num in nums2 . iter () {
233
- is_in_nums2 [to_index (num )] = true ;
234
- if ! is_in_nums1 [to_index (num )] {
235
- res2 . push (num );
236
- is_in_nums1 [to_index (num )] = true ;
237
- }
238
- }
239
- for & num in nums1 . iter () {
240
- if ! is_in_nums2 [to_index (num )] {
241
- res1 . push (num );
242
- is_in_nums2 [to_index (num )] = true ;
243
- }
244
- }
245
- vec! [res1 , res2 ]
200
+ return [array_keys($diff1), array_keys($diff2)];
246
201
}
247
202
}
248
203
```
0 commit comments