Skip to content

Commit 00cad4e

Browse files
committed
15-3sum.md Separated Chinese and English solutions.
1 parent c663a05 commit 00cad4e

File tree

3 files changed

+88
-217
lines changed

3 files changed

+88
-217
lines changed

en/1-1000/15-3sum.md

+10-87
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
1-
# 15. 3Sum - LeetCode Solution
2-
LeetCode problem link: [15. 3Sum](https://leetcode.com/problems/3sum),
3-
[15. 三数之和](https://leetcode.cn/problems/3sum)
1+
# 15. 3Sum - LeetCode Solution Best Practice
2+
LeetCode link: [15. 3Sum](https://leetcode.com/problems/3sum), difficulty: **Medium**
43

5-
[中文题解](#中文题解)
6-
7-
## LeetCode problem description
4+
## Description of "15. 3Sum"
85
Given an integer array nums, return all the triplets `[nums[i], nums[j], nums[k]]` such that `i != j`, `i != k`, and `j != k`, and `nums[i] + nums[j] + nums[k] == 0`.
96

107
Notice that the solution set must not contain duplicate triplets.
118

12-
Difficulty: **Medium**
13-
149
### [Example 1]
1510
**Input**: `nums = [-1,0,1,2,-1,-4]`
1611

@@ -43,6 +38,7 @@ Notice that the order of the output and the order of the triplets does not matte
4338
- `3 <= nums.length <= 3000`
4439
- `-10**5 <= nums[i] <= 10**5`
4540

41+
### [Hints]
4642
<details>
4743
<summary>Hint 1</summary>
4844
So, we essentially need to find three numbers x, y, and z such that they add up to the given value. If we fix one of the numbers say x, we are left with the two-sum problem at hand!
@@ -59,8 +55,6 @@ Notice that the order of the output and the order of the triplets does not matte
5955
</details>
6056

6157
## Intuition
62-
[中文题解](#中文题解)
63-
6458
1. The `sum` of three numbers equals `0`, which is equivalent to the `sum` of _two numbers_ equaling the _**negative** third number_.
6559
2. There are two options:
6660
1. First `determine two numbers` and `find the third number`
@@ -92,12 +86,6 @@ for (i = 0; i < nums.length; i++) {
9286
* Time: `O(N * N)`.
9387
* Space: `O(N)`.
9488

95-
## Java
96-
```java
97-
// Welcome to create a PR to complete the code of this language, thanks!
98-
```
99-
100-
10189
## Python
10290
### Solution 1: Use Map (complex and slow)
10391
```python
@@ -178,6 +166,11 @@ class Solution:
178166
// Welcome to create a PR to complete the code of this language, thanks!
179167
```
180168

169+
## Java
170+
```java
171+
// Welcome to create a PR to complete the code of this language, thanks!
172+
```
173+
181174
## JavaScript
182175
```javascript
183176
// Welcome to create a PR to complete the code of this language, thanks!
@@ -198,77 +191,7 @@ class Solution:
198191
# Welcome to create a PR to complete the code of this language, thanks!
199192
```
200193

201-
## C
202-
```c
203-
// Welcome to create a PR to complete the code of this language, thanks!
204-
```
205-
206-
## Kotlin
207-
```kotlin
208-
// Welcome to create a PR to complete the code of this language, thanks!
209-
```
210-
211-
## Swift
212-
```swift
213-
// Welcome to create a PR to complete the code of this language, thanks!
194+
## C, Kotlin, Swift, Rust or other languages
214195
```
215-
216-
## Rust
217-
```rust
218196
// Welcome to create a PR to complete the code of this language, thanks!
219197
```
220-
221-
## Other languages
222-
```
223-
// Welcome to create a PR to complete the code of this language, thanks!
224-
```
225-
226-
## 问题描述
227-
[15. 三数之和](https://leetcode.cn/problems/3sum), 难度: **中等**
228-
229-
给你一个整数数组 `nums` ,判断是否存在三元组 `[nums[i], nums[j], nums[k]]` 满足 `i != j``i != k``j != k` ,同时还满足 `nums[i] + nums[j] + nums[k] == 0` 。请你返回所有和为 `0` 且不重复的三元组。
230-
231-
**注意**:答案中****可以包含**重复**的三元组。
232-
233-
### [示例 1]
234-
**输入**: `nums = [-1,0,1,2,-1,-4]`
235-
236-
**输出**: `[[-1,-1,2],[-1,0,1]]`
237-
238-
**解释**:
239-
```
240-
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0 。
241-
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0 。
242-
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0 。
243-
不同的三元组是 [-1,0,1] 和 [-1,-1,2] 。
244-
注意,输出的顺序和三元组的顺序并不重要。
245-
```
246-
247-
# 中文题解
248-
## 思路
249-
1. 三个数相加等于0,等同于`两个数相加的和`等于`负的第三个数`
250-
2. 有两种方案可供选择:
251-
1. 先定下两个数,查找第三个数
252-
2. 先定下一个数,查找另外两个数
253-
3. 如果选择`方案1`,需要用到`Map`。因为需要对`nums`去重复;在`Map`中查找`第三个数`时,还需要避开已经定下的`两个数`,实现起来会比较麻烦。
254-
4. 如果选择`方案2`,查找另外两个数时,需要用到`two pointers`算法。
255-
5. 对于`方案1`,仅给出了`Python`示例代码。下文重点讲`方案2`
256-
257-
## 步骤
258-
1.`nums`进行排序。
259-
2. 遍历`nums`
260-
3. 伪代码:
261-
```javascript
262-
for (i = 0; i < nums.length; i++) {
263-
left = i + 1
264-
right = nums.length - 1
265-
266-
while (left < right) {
267-
if (condition1) {
268-
left += 1
269-
} else (condition2) {
270-
right -= 1
271-
}
272-
}
273-
}
274-
```

en/3001-4000/unorganized.md

+29-4
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,37 @@ The improved way with a queue is commonly more efficient. Relaxing **All Edges**
6262
## Others
6363
* Find all the prime numbers within 1000000.
6464

65-
### Skipped problems/solutions
65+
## Skipped problems/solutions
6666
- 1047 https://leetcode.cn/problems/remove-all-adjacent-duplicates-in-string/
6767
- 150 https://leetcode.cn/problems/evaluate-reverse-polish-notation/
6868
- 239 https://leetcode.cn/problems/sliding-window-maximum/ tag `monotonic queue`
6969
- 347 https://leetcode.cn/problems/top-k-frequent-elements/ tag `heap sort`
70-
-
71-
# Features
72-
* Add Google translate as a link. Link example: https://github-com.translate.goog/gazeldx/leetcode-solutions/blob/main/solutions/1-1000/122-best-time-to-buy-and-sell-stock-ii.md?_x_tr_sl=auto&_x_tr_tl=zh-CN&_x_tr_hl=en&_x_tr_pto=wapp
70+
71+
### Binary Tree
72+
- 144 https://leetcode.cn/problems/binary-tree-preorder-traversal/
73+
- 94 https://leetcode.cn/problems/binary-tree-inorder-traversal/
74+
- 145 https://leetcode.cn/problems/binary-tree-postorder-traversal/
75+
- 102 https://leetcode.cn/problems/binary-tree-level-order-traversal/
76+
- 107 https://leetcode.cn/problems/binary-tree-level-order-traversal-ii/
77+
- 199 https://leetcode.cn/problems/binary-tree-right-side-view/
78+
- 637 https://leetcode.cn/problems/average-of-levels-in-binary-tree/submissions/594783150/
79+
- 429 https://leetcode.cn/problems/n-ary-tree-level-order-traversal/
80+
- 515 https://leetcode.cn/problems/find-largest-value-in-each-tree-row/
81+
- 116 https://leetcode.cn/problems/populating-next-right-pointers-in-each-node/
82+
- 117 https://leetcode.cn/problems/populating-next-right-pointers-in-each-node-ii/
83+
- 111 https://leetcode.cn/problems/minimum-depth-of-binary-tree/
84+
85+
- 104 https://leetcode.cn/problems/maximum-depth-of-binary-tree/
86+
- 226 https://leetcode.cn/problems/invert-binary-tree/
87+
- 101 https://leetcode.cn/problems/symmetric-tree/
88+
- 100 https://leetcode.cn/problems/same-tree/description/
89+
- 572 https://leetcode.cn/problems/subtree-of-another-tree/
90+
91+
- 222 https://leetcode.cn/problems/count-complete-tree-nodes/
92+
- 110 https://leetcode.cn/problems/balanced-binary-tree/ 2 ways
93+
- 257 https://leetcode.cn/problems/binary-tree-paths/description/
94+
- 404 https://leetcode.cn/problems/sum-of-left-leaves/
95+
96+
### Failed in 2 rounds
97+
- 222 https://leetcode.cn/problems/count-complete-tree-nodes/
7398

0 commit comments

Comments
 (0)