Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit00cad4e

Browse files
committed
15-3sum.md Separated Chinese and English solutions.
1 parentc663a05 commit00cad4e

File tree

3 files changed

+88
-217
lines changed

3 files changed

+88
-217
lines changed

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

Lines changed: 10 additions & 87 deletions
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

Lines changed: 29 additions & 4 deletions
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
- 1047https://leetcode.cn/problems/remove-all-adjacent-duplicates-in-string/
6767
- 150https://leetcode.cn/problems/evaluate-reverse-polish-notation/
6868
- 239https://leetcode.cn/problems/sliding-window-maximum/ tag`monotonic queue`
6969
- 347https://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+
- 144https://leetcode.cn/problems/binary-tree-preorder-traversal/
73+
- 94https://leetcode.cn/problems/binary-tree-inorder-traversal/
74+
- 145https://leetcode.cn/problems/binary-tree-postorder-traversal/
75+
- 102https://leetcode.cn/problems/binary-tree-level-order-traversal/
76+
- 107https://leetcode.cn/problems/binary-tree-level-order-traversal-ii/
77+
- 199https://leetcode.cn/problems/binary-tree-right-side-view/
78+
- 637https://leetcode.cn/problems/average-of-levels-in-binary-tree/submissions/594783150/
79+
- 429https://leetcode.cn/problems/n-ary-tree-level-order-traversal/
80+
- 515https://leetcode.cn/problems/find-largest-value-in-each-tree-row/
81+
- 116https://leetcode.cn/problems/populating-next-right-pointers-in-each-node/
82+
- 117https://leetcode.cn/problems/populating-next-right-pointers-in-each-node-ii/
83+
- 111https://leetcode.cn/problems/minimum-depth-of-binary-tree/
84+
85+
- 104https://leetcode.cn/problems/maximum-depth-of-binary-tree/
86+
- 226https://leetcode.cn/problems/invert-binary-tree/
87+
- 101https://leetcode.cn/problems/symmetric-tree/
88+
- 100https://leetcode.cn/problems/same-tree/description/
89+
- 572https://leetcode.cn/problems/subtree-of-another-tree/
90+
91+
- 222https://leetcode.cn/problems/count-complete-tree-nodes/
92+
- 110https://leetcode.cn/problems/balanced-binary-tree/ 2 ways
93+
- 257https://leetcode.cn/problems/binary-tree-paths/description/
94+
- 404https://leetcode.cn/problems/sum-of-left-leaves/
95+
96+
###Failed in 2 rounds
97+
- 222https://leetcode.cn/problems/count-complete-tree-nodes/
7398

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp