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 ) ,难度:** 中等** 。
43
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) ` 能满足:
96
107* ` 0 <= i, j, k, l < n `
118* ` nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0 `
129
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] `
1712
18- ** Output ** :` 2 `
13+ ** 输出 ** :` 2 `
1914
20- ** Explanation ** :
15+ ** 解释 ** :
2116```
22- The two tuples are:
17+ 两个元组如下:
23181. (0, 0, 0, 1) -> nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0
24192. (1, 1, 0, 0) -> nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0
2520```
2621
27- ###[ Example 2]
28- ** Input ** :` nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0] `
22+ ###[ 示例 2]
23+ ** 输入 ** :` nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0] `
2924
30- ** Output ** :` 1 `
25+ ** 输出 ** :` 1 `
3126
32- ###[ Constraints ]
27+ ###[ 约束 ]
3328- ` n == nums1.length `
3429- ` n == nums2.length `
3530- ` n == nums3.length `
3631- ` n == nums4.length `
3732- ` 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 `
3934
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 ` 计入总数。
4639
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 ` 。
4942``` python
5043num_to_count= defaultdict(int )
5144
@@ -54,7 +47,7 @@ for num1 in nums1:
5447 num_to_count[num1+ num2]+= 1
5548```
5649
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 ` 计入总数。
5851``` python
5952result= 0
6053
@@ -65,9 +58,9 @@ for num3 in nums3:
6558return result
6659```
6760
68- ##Complexity
69- * Time :` O(n * n) ` .
70- * Space :` O(n) ` .
61+ ##复杂度
62+ * 时间 :` O(n * n) ` .
63+ * 空间 :` O(n) ` .
7164
7265##Java
7366``` java
229222```
230223// Welcome to create a PR to complete the code of this language, thanks!
231224```
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 num1in nums1:
265- for num2in 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 num3in nums3:
274- for num4in nums4:
275- result+= num_to_count[- (num3+ num4)]
276-
277- return result
278- ```