1
- # 454. 4Sum II - Best Practices of LeetCode Solutions
2
- LeetCode link: [ 454. 4Sum II] ( https://leetcode.com/problems/4sum-ii ) ,
3
- [ 454. 四数相加 II] ( https://leetcode.cn/problems/4sum-ii )
1
+ # 454. 四数相加 II - 力扣题解最佳实践
2
+ 力扣链接:[ 454. 四数相加 II] ( https://leetcode.cn/problems/4sum-ii ) ,难度:** 中等** 。
4
3
5
- [ 中文题解] ( #中文题解 )
6
-
7
- ## LeetCode problem description
8
- Given four integer arrays ` nums1 ` , ` nums2 ` , ` nums3 ` , and ` nums4 ` all of length ` n ` , return the number of tuples` (i, j, k, l) ` such that:
4
+ ## 力扣“454. 四数相加 II”问题描述
5
+ 给你四个整数数组 ` nums1 ` 、` nums2 ` 、` nums3 ` 和 ` nums4 ` ,数组长度都是 ` n ` ,请你计算有多少个元组 ` (i, j, k, l) ` 能满足:
9
6
10
7
* ` 0 <= i, j, k, l < n `
11
8
* ` nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0 `
12
9
13
- Difficulty: ** Medium**
14
-
15
- ### [ Example 1]
16
- ** Input** : ` nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2] `
10
+ ### [ 示例 1]
11
+ ** 输入** : ` nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2] `
17
12
18
- ** Output ** : ` 2 `
13
+ ** 输出 ** : ` 2 `
19
14
20
- ** Explanation ** :
15
+ ** 解释 ** :
21
16
```
22
- The two tuples are:
17
+ 两个元组如下:
23
18
1. (0, 0, 0, 1) -> nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0
24
19
2. (1, 1, 0, 0) -> nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0
25
20
```
26
21
27
- ### [ Example 2]
28
- ** Input ** : ` nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0] `
22
+ ### [ 示例 2]
23
+ ** 输入 ** : ` nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0] `
29
24
30
- ** Output ** : ` 1 `
25
+ ** 输出 ** : ` 1 `
31
26
32
- ### [ Constraints ]
27
+ ### [ 约束 ]
33
28
- ` n == nums1.length `
34
29
- ` n == nums2.length `
35
30
- ` n == nums3.length `
36
31
- ` n == nums4.length `
37
32
- ` 1 <= n <= 200 `
38
- - ` -2** 28 <= nums1[i], nums2[i], nums3[i], nums4[i] <= 2** 28 `
33
+ - ` -2^ 28 <= nums1[i], nums2[i], nums3[i], nums4[i] <= 2^ 28 `
39
34
40
- ## Intuition
41
- [ 中文题解] ( #中文题解 )
42
-
43
- 1 . Because the final requirement is to take one number from each group of numbers, the four groups of numbers can be split into ** two groups of two** .
44
- 2 . Count the number of each ` sum ` . Use ` Map ` to store, ` key ` is ` sum ` , ` value ` is ` count ` .
45
- 3 . Iterate over ` nums3 ` and ` nums4 ` , if ` -(num3 + num4) ` exists in ` keys ` of ` Map ` , then ` count ` is included in the total.
35
+ ## 思路
36
+ 1 . 因为最终要求是每组数中各取一个数,所以可以把四组数拆分成两个两组数。
37
+ 2 . 统计出每个` 和 ` 值对应的个数。使用` Map ` 储存,` key ` 为` 和 ` ,` value ` 为` count ` 。
38
+ 3 . 遍历` nums3 ` 和` nums4 ` ,如果` -(num3 + num4) ` 存在于` Map ` 的` keys ` 中,则` count ` 计入总数。
46
39
47
- ## Steps
48
- 1 . Count the number of each ` sum ` . Use ` Map ` to store, ` key ` is ` sum ` , ` value ` is ` count ` .
40
+ ## 步骤
41
+ 1 . 统计出每个 ` 和 ` 值对应的个数。使用 ` Map ` 储存, ` key ` 为 ` 和 ` , ` value ` 为 ` count ` 。
49
42
``` python
50
43
num_to_count = defaultdict(int )
51
44
@@ -54,7 +47,7 @@ for num1 in nums1:
54
47
num_to_count[num1 + num2] += 1
55
48
```
56
49
57
- 2 . Iterate over ` nums3 ` and ` nums4 ` , if ` -(num3 + num4) ` exists in ` keys ` of ` Map ` , then ` count ` is included in the total.
50
+ 2 . 遍历 ` nums3 ` 和 ` nums4 ` ,如果 ` -(num3 + num4) ` 存在于 ` Map ` 的 ` keys ` 中,则 ` count ` 计入总数。
58
51
``` python
59
52
result = 0
60
53
@@ -65,9 +58,9 @@ for num3 in nums3:
65
58
return result
66
59
```
67
60
68
- ## Complexity
69
- * Time : ` O(n * n) ` .
70
- * Space : ` O(n) ` .
61
+ ## 复杂度
62
+ * 时间 : ` O(n * n) ` .
63
+ * 空间 : ` O(n) ` .
71
64
72
65
## Java
73
66
``` java
229
222
```
230
223
// Welcome to create a PR to complete the code of this language, thanks!
231
224
```
232
-
233
- ## 问题描述
234
- 给你四个整数数组 ` nums1 ` 、` nums2 ` 、` nums3 ` 和 ` nums4 ` ,数组长度都是 ` n ` ,请你计算有多少个元组 ` (i, j, k, l) ` 能满足:
235
-
236
- * ` 0 <= i, j, k, l < n `
237
- * ` nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0 `
238
-
239
- 难度: ** 中等**
240
-
241
- ### [ 示例 1]
242
- ** 输入** : ` nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2] `
243
-
244
- ** 输出** : ` 2 `
245
-
246
- ** 解释** :
247
- ```
248
- 两个元组如下:
249
- 1. (0, 0, 0, 1) -> nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0
250
- 2. (1, 1, 0, 0) -> nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0
251
- ```
252
-
253
- # 中文题解
254
- ## 思路
255
- 1 . 因为最终要求是每组数中各取一个数,所以可以把四组数拆分成两个两组数。
256
- 2 . 统计出每个` 和 ` 值对应的个数。使用` Map ` 储存,` key ` 为` 和 ` ,` value ` 为` count ` 。
257
- 3 . 遍历` nums3 ` 和` nums4 ` ,如果` -(num3 + num4) ` 存在于` Map ` 的` keys ` 中,则` count ` 计入总数。
258
-
259
- ## 步骤
260
- 1 . 统计出每个` 和 ` 值对应的个数。使用` Map ` 储存,` key ` 为` 和 ` ,` value ` 为` count ` 。
261
- ``` python
262
- num_to_count = defaultdict(int )
263
-
264
- for num1 in nums1:
265
- for num2 in nums2:
266
- num_to_count[num1 + num2] += 1
267
- ```
268
-
269
- 2 . 遍历` nums3 ` 和` nums4 ` ,如果` -(num3 + num4) ` 存在于` Map ` 的` keys ` 中,则` count ` 计入总数。
270
- ``` python
271
- result = 0
272
-
273
- for num3 in nums3:
274
- for num4 in nums4:
275
- result += num_to_count[- (num3 + num4)]
276
-
277
- return result
278
- ```
0 commit comments