Skip to content

Commit 9bb6d44

Browse files
authored
feat: add solutions to lc problem: No.2215 (doocs#2483)
No.2215.Find the Difference of Two Arrays
1 parent 8170963 commit 9bb6d44

File tree

8 files changed

+85
-215
lines changed

8 files changed

+85
-215
lines changed

solution/2200-2299/2215.Find the Difference of Two Arrays/README.md

+29-74
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@ nums2 中的每个整数都在 nums1 中出现,因此,answer[1] = [] 。
4949

5050
## 解法
5151

52-
### 方法一
52+
### 方法一:哈希表
53+
54+
我们定义两个哈希表 $s1$ 和 $s2$ 分别存储数组 $nums1$ 和 $nums2$ 中的元素。然后遍历 $s1$ 中的每个元素,如果该元素不在 $s2$ 中,那么将其加入到答案的第一个列表中。同理,遍历 $s2$ 中的每个元素,如果该元素不在 $s1$ 中,那么将其加入到答案的第二个列表中。
55+
56+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组的长度。
5357

5458
<!-- tabs:start -->
5559

@@ -65,8 +69,6 @@ class Solution {
6569
public List<List<Integer>> findDifference(int[] nums1, int[] nums2) {
6670
Set<Integer> s1 = convert(nums1);
6771
Set<Integer> s2 = convert(nums2);
68-
69-
List<List<Integer>> ans = new ArrayList<>();
7072
List<Integer> l1 = new ArrayList<>();
7173
List<Integer> l2 = new ArrayList<>();
7274
for (int v : s1) {
@@ -79,9 +81,7 @@ class Solution {
7981
l2.add(v);
8082
}
8183
}
82-
ans.add(l1);
83-
ans.add(l2);
84-
return ans;
84+
return List.of(l1, l2);
8585
}
8686

8787
private Set<Integer> convert(int[] nums) {
@@ -101,12 +101,16 @@ public:
101101
unordered_set<int> s1(nums1.begin(), nums1.end());
102102
unordered_set<int> s2(nums2.begin(), nums2.end());
103103
vector<vector<int>> ans(2);
104-
for (int v : s1)
105-
if (!s2.count(v))
104+
for (int v : s1) {
105+
if (!s2.contains(v)) {
106106
ans[0].push_back(v);
107-
for (int v : s2)
108-
if (!s1.count(v))
107+
}
108+
}
109+
for (int v : s2) {
110+
if (!s1.contains(v)) {
109111
ans[1].push_back(v);
112+
}
113+
}
110114
return ans;
111115
}
112116
};
@@ -138,10 +142,11 @@ func findDifference(nums1 []int, nums2 []int) [][]int {
138142

139143
```ts
140144
function findDifference(nums1: number[], nums2: number[]): number[][] {
141-
return [
142-
[...new Set<number>(nums1.filter(v => !nums2.includes(v)))],
143-
[...new Set<number>(nums2.filter(v => !nums1.includes(v)))],
144-
];
145+
const s1: Set<number> = new Set(nums1);
146+
const s2: Set<number> = new Set(nums2);
147+
nums1.forEach(num => s2.delete(num));
148+
nums2.forEach(num => s1.delete(num));
149+
return [Array.from(s1), Array.from(s2)];
145150
}
146151
```
147152

@@ -174,15 +179,11 @@ impl Solution {
174179
* @return {number[][]}
175180
*/
176181
var findDifference = function (nums1, nums2) {
177-
let ans1 = new Set(nums1),
178-
ans2 = new Set(nums2);
179-
for (let num of nums1) {
180-
ans2.delete(num);
181-
}
182-
for (let num of nums2) {
183-
ans1.delete(num);
184-
}
185-
return [Array.from(ans1), Array.from(ans2)];
182+
const s1 = new Set(nums1);
183+
const s2 = new Set(nums2);
184+
nums1.forEach(num => s2.delete(num));
185+
nums2.forEach(num => s1.delete(num));
186+
return [Array.from(s1), Array.from(s2)];
186187
};
187188
```
188189

@@ -194,59 +195,13 @@ class Solution {
194195
* @return Integer[][]
195196
*/
196197
function findDifference($nums1, $nums2) {
197-
$rs = [[], []];
198-
$hashtable1 = array_flip(array_unique($nums1));
199-
$hashtable2 = array_flip(array_unique($nums2));
200-
for ($m = 0; $m < count($nums1); $m++) {
201-
if (!isset($hashtable2[$nums1[$m]])) {
202-
$rs[0][$m] = $nums1[$m];
203-
$hashtable2[$nums1[$m]] = 1;
204-
}
205-
}
206-
for ($n = 0; $n < count($nums2); $n++) {
207-
if (!isset($hashtable1[$nums2[$n]])) {
208-
$rs[1][$n] = $nums2[$n];
209-
$hashtable1[$nums2[$n]] = 1;
210-
}
211-
}
212-
return $rs;
213-
}
214-
}
215-
```
198+
$s1 = array_flip($nums1);
199+
$s2 = array_flip($nums2);
216200

217-
<!-- tabs:end -->
218-
219-
### 方法二
220-
221-
<!-- tabs:start -->
201+
$diff1 = array_diff_key($s1, $s2);
202+
$diff2 = array_diff_key($s2, $s1);
222203

223-
```rust
224-
impl Solution {
225-
pub fn find_difference(nums1: Vec<i32>, nums2: Vec<i32>) -> Vec<Vec<i32>> {
226-
const N: usize = 2001;
227-
let to_index = |i| (i as usize) + 1000;
228-
229-
let mut is_in_nums1 = [false; N];
230-
let mut is_in_nums2 = [false; N];
231-
let mut res1 = vec![];
232-
let mut res2 = vec![];
233-
for &num in nums1.iter() {
234-
is_in_nums1[to_index(num)] = true;
235-
}
236-
for &num in nums2.iter() {
237-
is_in_nums2[to_index(num)] = true;
238-
if !is_in_nums1[to_index(num)] {
239-
res2.push(num);
240-
is_in_nums1[to_index(num)] = true;
241-
}
242-
}
243-
for &num in nums1.iter() {
244-
if !is_in_nums2[to_index(num)] {
245-
res1.push(num);
246-
is_in_nums2[to_index(num)] = true;
247-
}
248-
}
249-
vec![res1, res2]
204+
return [array_keys($diff1), array_keys($diff2)];
250205
}
251206
}
252207
```

solution/2200-2299/2215.Find the Difference of Two Arrays/README_EN.md

+29-74
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ Every integer in nums2 is present in nums1. Therefore, answer[1] = [].
4545

4646
## Solutions
4747

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.
4953

5054
<!-- tabs:start -->
5155

@@ -61,8 +65,6 @@ class Solution {
6165
public List<List<Integer>> findDifference(int[] nums1, int[] nums2) {
6266
Set<Integer> s1 = convert(nums1);
6367
Set<Integer> s2 = convert(nums2);
64-
65-
List<List<Integer>> ans = new ArrayList<>();
6668
List<Integer> l1 = new ArrayList<>();
6769
List<Integer> l2 = new ArrayList<>();
6870
for (int v : s1) {
@@ -75,9 +77,7 @@ class Solution {
7577
l2.add(v);
7678
}
7779
}
78-
ans.add(l1);
79-
ans.add(l2);
80-
return ans;
80+
return List.of(l1, l2);
8181
}
8282

8383
private Set<Integer> convert(int[] nums) {
@@ -97,12 +97,16 @@ public:
9797
unordered_set<int> s1(nums1.begin(), nums1.end());
9898
unordered_set<int> s2(nums2.begin(), nums2.end());
9999
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)) {
102102
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)) {
105107
ans[1].push_back(v);
108+
}
109+
}
106110
return ans;
107111
}
108112
};
@@ -134,10 +138,11 @@ func findDifference(nums1 []int, nums2 []int) [][]int {
134138

135139
```ts
136140
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)];
141146
}
142147
```
143148

@@ -170,15 +175,11 @@ impl Solution {
170175
* @return {number[][]}
171176
*/
172177
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)];
182183
};
183184
```
184185

@@ -190,59 +191,13 @@ class Solution {
190191
* @return Integer[][]
191192
*/
192193
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);
212196

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);
218199

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)];
246201
}
247202
}
248203
```

solution/2200-2299/2215.Find the Difference of Two Arrays/Solution.cpp

+8-4
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@ class Solution {
44
unordered_set<int> s1(nums1.begin(), nums1.end());
55
unordered_set<int> s2(nums2.begin(), nums2.end());
66
vector<vector<int>> ans(2);
7-
for (int v : s1)
8-
if (!s2.count(v))
7+
for (int v : s1) {
8+
if (!s2.contains(v)) {
99
ans[0].push_back(v);
10-
for (int v : s2)
11-
if (!s1.count(v))
10+
}
11+
}
12+
for (int v : s2) {
13+
if (!s1.contains(v)) {
1214
ans[1].push_back(v);
15+
}
16+
}
1317
return ans;
1418
}
1519
};

solution/2200-2299/2215.Find the Difference of Two Arrays/Solution.java

+1-5
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ class Solution {
22
public List<List<Integer>> findDifference(int[] nums1, int[] nums2) {
33
Set<Integer> s1 = convert(nums1);
44
Set<Integer> s2 = convert(nums2);
5-
6-
List<List<Integer>> ans = new ArrayList<>();
75
List<Integer> l1 = new ArrayList<>();
86
List<Integer> l2 = new ArrayList<>();
97
for (int v : s1) {
@@ -16,9 +14,7 @@ public List<List<Integer>> findDifference(int[] nums1, int[] nums2) {
1614
l2.add(v);
1715
}
1816
}
19-
ans.add(l1);
20-
ans.add(l2);
21-
return ans;
17+
return List.of(l1, l2);
2218
}
2319

2420
private Set<Integer> convert(int[] nums) {

solution/2200-2299/2215.Find the Difference of Two Arrays/Solution.js

+5-9
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,9 @@
44
* @return {number[][]}
55
*/
66
var findDifference = function (nums1, nums2) {
7-
let ans1 = new Set(nums1),
8-
ans2 = new Set(nums2);
9-
for (let num of nums1) {
10-
ans2.delete(num);
11-
}
12-
for (let num of nums2) {
13-
ans1.delete(num);
14-
}
15-
return [Array.from(ans1), Array.from(ans2)];
7+
const s1 = new Set(nums1);
8+
const s2 = new Set(nums2);
9+
nums1.forEach(num => s2.delete(num));
10+
nums2.forEach(num => s1.delete(num));
11+
return [Array.from(s1), Array.from(s2)];
1612
};

0 commit comments

Comments
 (0)