Skip to content

Commit bb6cb6e

Browse files
committed
454-4sum-ii.md Separated Chinese solutions.
1 parent f7d056a commit bb6cb6e

File tree

3 files changed

+28
-136
lines changed

3 files changed

+28
-136
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ You can skip the more difficult problems and do them later.
121121
- [1334. Find the City With the Smallest Number of Neighbors at a Threshold Distance](en/1001-2000/1334-find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance.md) was solved in _Python_.
122122

123123
# Others
124-
- [433. Minimum Genetic Mutation](en/1-1000/433-minimum-genetic-mutation.md) was solved in _Python_ and 2 ways.
124+
- [433. Minimum Genetic Mutation](en/1-1000/433-minimum-genetic-mutation.md) was solved in _Python_ and 3 ways.
125125

126-
TODO: Add A* for 433 and 127?
126+
TODO: Add A* for 127?
127127

128128
More LeetCode problems will be added soon.

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

Lines changed: 2 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
# 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)
2+
LeetCode link: [454. 4Sum II](https://leetcode.com/problems/problem), difficulty: **Medium**.
43

5-
[中文题解](#中文题解)
6-
7-
## LeetCode problem description
4+
## LeetCode description of "454. 4Sum II"
85
Given four integer arrays `nums1`, `nums2`, `nums3`, and `nums4` all of length `n`, return the number of tuples` (i, j, k, l)` such that:
96

107
* `0 <= i, j, k, l < n`
118
* `nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0`
129

13-
Difficulty: **Medium**
14-
1510
### [Example 1]
1611
**Input**: `nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2]`
1712

@@ -38,8 +33,6 @@ The two tuples are:
3833
- `-2**28 <= nums1[i], nums2[i], nums3[i], nums4[i] <= 2**28`
3934

4035
## Intuition
41-
[中文题解](#中文题解)
42-
4336
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**.
4437
2. Count the number of each `sum`. Use `Map` to store, `key` is `sum`, `value` is `count`.
4538
3. Iterate over `nums3` and `nums4`, if `-(num3 + num4)` exists in `keys` of `Map`, then `count` is included in the total.
@@ -229,50 +222,3 @@ end
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 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-
```

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

Lines changed: 24 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,44 @@
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+
两个元组如下:
2318
1. (0, 0, 0, 1) -> nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0
2419
2. (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
5043
num_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
5952
result = 0
6053

@@ -65,9 +58,9 @@ for num3 in nums3:
6558
return 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
@@ -229,50 +222,3 @@ end
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 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

Comments
 (0)