Skip to content

Commit 66eb807

Browse files
committed
Updated 454-4sum-ii.md
1 parent e7f20e7 commit 66eb807

File tree

2 files changed

+40
-34
lines changed

2 files changed

+40
-34
lines changed

en/1-1000/454-4sum-ii.md

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
LeetCode link: [454. 4Sum II](https://leetcode.com/problems/problem), difficulty: **Medium**.
33

44
## LeetCode description of "454. 4Sum II"
5-
Given four integer arrays `nums1`, `nums2`, `nums3`, and `nums4` all of length `n`, return the number of tuples` (i, j, k, l)` such that:
5+
Given four integer arrays `nums1`, `nums2`, `nums3`, and `nums4` all of length `n`, return the number of tuples `(i, j, k, l)` such that:
66

7-
* `0 <= i, j, k, l < n`
8-
* `nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0`
7+
- `0 <= i, j, k, l < n`
8+
- `nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0`
99

1010
### [Example 1]
1111
**Input**: `nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2]`
@@ -30,33 +30,36 @@ The two tuples are:
3030
- `n == nums3.length`
3131
- `n == nums4.length`
3232
- `1 <= n <= 200`
33-
- `-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`
3434

3535
## Intuition
3636
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**.
3737
2. Count the number of each `sum`. Use `Map` to store, `key` is `sum`, `value` is `count`.
3838
3. Iterate over `nums3` and `nums4`, if `-(num3 + num4)` exists in `keys` of `Map`, then `count` is included in the total.
3939

4040
## Steps
41+
4142
1. Count the number of each `sum`. Use `Map` to store, `key` is `sum`, `value` is `count`.
42-
```python
43-
num_to_count = defaultdict(int)
4443

45-
for num1 in nums1:
46-
for num2 in nums2:
47-
num_to_count[num1 + num2] += 1
48-
```
44+
```python
45+
num_to_count = defaultdict(int)
46+
47+
for num1 in nums1:
48+
for num2 in nums2:
49+
num_to_count[num1 + num2] += 1
50+
```
4951

5052
2. Iterate over `nums3` and `nums4`, if `-(num3 + num4)` exists in `keys` of `Map`, then `count` is included in the total.
51-
```python
52-
result = 0
53-
54-
for num3 in nums3:
55-
for num4 in nums4:
56-
result += num_to_count[-(num3 + num4)]
5753

58-
return result
59-
```
54+
```python
55+
result = 0
56+
57+
for num3 in nums3:
58+
for num4 in nums4:
59+
result += num_to_count[-(num3 + num4)]
60+
61+
return result
62+
```
6063

6164
## Complexity
6265
* Time: `O(n * n)`.

zh/1-1000/454-4sum-ii.md

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
## 力扣“454. 四数相加 II”问题描述
55
给你四个整数数组 `nums1``nums2``nums3``nums4` ,数组长度都是 `n` ,请你计算有多少个元组 `(i, j, k, l)` 能满足:
66

7-
* `0 <= i, j, k, l < n`
8-
* `nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0`
7+
- `0 <= i, j, k, l < n`
8+
- `nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0`
99

1010
### [示例 1]
1111
**输入**: `nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2]`
@@ -38,25 +38,28 @@
3838
3. 遍历`nums3``nums4`,如果`-(num3 + num4)`存在于`Map``keys`中,则`count`计入总数。
3939

4040
## 步骤
41+
4142
1. 统计出每个``值对应的个数。使用`Map`储存,`key````value``count`
42-
```python
43-
num_to_count = defaultdict(int)
4443

45-
for num1 in nums1:
46-
for num2 in nums2:
47-
num_to_count[num1 + num2] += 1
48-
```
44+
```python
45+
num_to_count = defaultdict(int)
46+
47+
for num1 in nums1:
48+
for num2 in nums2:
49+
num_to_count[num1 + num2] += 1
50+
```
4951

5052
2. 遍历`nums3``nums4`,如果`-(num3 + num4)`存在于`Map``keys`中,则`count`计入总数。
51-
```python
52-
result = 0
53-
54-
for num3 in nums3:
55-
for num4 in nums4:
56-
result += num_to_count[-(num3 + num4)]
5753

58-
return result
59-
```
54+
```python
55+
result = 0
56+
57+
for num3 in nums3:
58+
for num4 in nums4:
59+
result += num_to_count[-(num3 + num4)]
60+
61+
return result
62+
```
6063

6164
## 复杂度
6265
* 时间: `O(n * n)`.

0 commit comments

Comments
 (0)