|
1 | | -# 18. 4Sum - LeetCode Solution |
2 | | -LeetCode problem link: [18. 4Sum](https://leetcode.com/problems/4sum), |
3 | | -[18. 四数之和](https://leetcode.cn/problems/4sum) |
| 1 | +# 18. 四数之和 - 力扣题解最佳实践 |
| 2 | +力扣链接:[18. 四数之和](https://leetcode.cn/problems/4sum), 难度: **中等**。 |
4 | 3 |
|
5 | | -[中文题解](#中文题解) |
6 | | - |
7 | | -## LeetCode problem description |
8 | | -Given an array `nums` of `n` integers, return _an array of all the **unique** quadruplets_ `[nums[a], nums[b], nums[c], nums[d]]` such that: |
| 4 | +## 力扣“18. 四数之和”问题描述 |
| 5 | +给你一个由 `n` 个整数组成的数组 `nums` ,和一个目标值 `target` 。请你找出并返回满足下述全部条件且**不重复**的四元组 `[nums[a], nums[b], nums[c], nums[d]]` (若两个四元组元素一一对应,则认为两个四元组重复): |
9 | 6 |
|
10 | | -* `0 <= a, b, c, d < n` |
11 | | -* `a`, `b`, `c`, and `d` are **distinct**. |
12 | | -* `nums[a] + nums[b] + nums[c] + nums[d] == target` |
13 | | -* You may return the answer in **any order**. |
| 7 | +- `0 <= a, b, c, d < n` |
| 8 | +- `a`、`b`、`c` 和 `d` **互不相同** |
| 9 | +- `nums[a] + nums[b] + nums[c] + nums[d] == target` |
14 | 10 |
|
15 | | -Difficulty: **Medium** |
| 11 | +你可以按 **任意顺序** 返回答案 。 |
16 | 12 |
|
17 | | -### [Example 1] |
18 | | -**Input**: `nums = [1,0,-1,0,-2,2], target = 0` |
| 13 | +### [示例 1] |
| 14 | +**输入**: `nums = [1,0,-1,0,-2,2], target = 0` |
19 | 15 |
|
20 | | -**Output**: `[[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]` |
| 16 | +**输出**: `[[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]` |
21 | 17 |
|
22 | | -### [Example 2] |
23 | | -**Input**: `nums = [2,2,2,2,2], target = 8` |
| 18 | +### [示例 2] |
| 19 | +**输入**: `nums = [2,2,2,2,2], target = 8` |
24 | 20 |
|
25 | | -**Output**: `[[2,2,2,2]]` |
| 21 | +**输出**: `[[2,2,2,2]]` |
26 | 22 |
|
27 | | -### [Constraints] |
| 23 | +### [约束] |
28 | 24 | - `1 <= nums.length <= 200` |
29 | 25 | - `-10**9 <= nums[i] <= 10**9` |
30 | 26 | - `-10**9 <= target <= 10**9` |
31 | 27 |
|
32 | | -## Intuition |
33 | | -[中文题解](#中文题解) |
34 | | - |
35 | | -1. The idea of this question is the same as [15. 3Sum](15-3sum.md), please click the link to view. |
36 | | -2. The difference is that `three numbers` becomes `four numbers`, and processing `four numbers` only requires **one more nested loop**. |
37 | | -3. In addition, the `target` parameter is added, which needs to be brought in as a condition during calculation. |
38 | | -4. You may have already seen that no matter it is `two numbers`, `three numbers` or `four numbers`, the `Two Pointers Technique` can be used. |
39 | | - |
40 | | -## Complexity |
41 | | -* Time: `O(n**3)`. |
42 | | -* Space: `O(n)`. |
| 28 | +## 思路 |
| 29 | +1. 本题思路同[15. 三数之和](15-3sum.md), 请点链接查看。 |
| 30 | +2. 区别是`三数`变`四数`,处理`四数`,只需要**多加一重嵌套的循环**。 |
| 31 | +3. 另外增加了`target`参数,计算时需要把它做为条件带入。 |
| 32 | +4. 你可能已经看出来了,不管它是`两数`、`三数`还是`四数`,都可以用`双指针技术`。 |
43 | 33 |
|
44 | | -## Java |
45 | | -```java |
46 | | -// Welcome to create a PR to complete the code of this language, thanks! |
47 | | -``` |
| 34 | +## 复杂度 |
| 35 | +* 时间:`O(N**3)`。 |
| 36 | +* 空间:`O(N)`。 |
48 | 37 |
|
49 | 38 | ## Python |
50 | 39 | ```python |
@@ -82,6 +71,11 @@ class Solution: |
82 | 71 | return list(results) |
83 | 72 | ``` |
84 | 73 |
|
| 74 | +## Java |
| 75 | +```java |
| 76 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 77 | +``` |
| 78 | + |
85 | 79 | ## C++ |
86 | 80 | ```cpp |
87 | 81 | // Welcome to create a PR to complete the code of this language, thanks! |
@@ -131,31 +125,3 @@ class Solution: |
131 | 125 | ``` |
132 | 126 | // Welcome to create a PR to complete the code of this language, thanks! |
133 | 127 | ``` |
134 | | - |
135 | | -## 力扣问题描述 |
136 | | -[18. 四数之和](https://leetcode.cn/problems/4sum), 难度: **中等**。 |
137 | | - |
138 | | -给你一个由 `n` 个整数组成的数组 `nums` ,和一个目标值 `target` 。请你找出并返回满足下述全部条件且**不重复**的四元组 `[nums[a], nums[b], nums[c], nums[d]]` (若两个四元组元素一一对应,则认为两个四元组重复): |
139 | | - |
140 | | -- `0 <= a, b, c, d < n` |
141 | | -- `a`、`b`、`c` 和 `d` **互不相同** |
142 | | -- `nums[a] + nums[b] + nums[c] + nums[d] == target` |
143 | | - |
144 | | -你可以按 **任意顺序** 返回答案 。 |
145 | | - |
146 | | -### [示例 1] |
147 | | -**输入**: `nums = [1,0,-1,0,-2,2], target = 0` |
148 | | - |
149 | | -**输出**: `[[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]` |
150 | | - |
151 | | -### [示例 2] |
152 | | -**输入**: `nums = [2,2,2,2,2], target = 8` |
153 | | - |
154 | | -**输出**: `[[2,2,2,2]]` |
155 | | - |
156 | | -# 中文题解 |
157 | | -## 思路 |
158 | | -1. 本题思路同[15. 三数之和](15-3sum.md), 请点链接查看。 |
159 | | -2. 区别是`三数`变`四数`,处理`四数`,只需要**多加一重嵌套的循环**。 |
160 | | -3. 另外增加了`target`参数,计算时需要把它做为条件带入。 |
161 | | -4. 你可能已经看出来了,不管它是`两数`、`三数`还是`四数`,都可以用`双指针技术`。 |
0 commit comments