Skip to content

Commit c663a05

Browse files
committed
Separated English and Chinese solutions.
1 parent db8d8a9 commit c663a05

File tree

3 files changed

+73
-168
lines changed

3 files changed

+73
-168
lines changed

en/1-1000/1-two-sum.md

Lines changed: 20 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
1-
# 1. Two Sum - LeetCode Solution
2-
LeetCode problem link: [1. Two Sum](https://leetcode.com/problems/two-sum),
3-
[1. 两数之和](https://leetcode.cn/problems/two-sum)
1+
# 1. Two Sum - LeetCode Solution Best Practice
2+
LeetCode link: [1. Two Sum](https://leetcode.com/problems/two-sum), difficulty: **Easy**
43

5-
[中文题解](#中文题解)
6-
7-
## LeetCode problem description
4+
## Description of "1. Two Sum"
85
Given an array of integers `nums` and an integer `target`, return _indices of the two numbers such that they add up to `target`_.
96

107
You may assume that each input would have **_exactly_ one solution**, and you may not use the same element twice.
118

129
You can return the answer in any order.
1310

14-
Difficulty: **Easy**
15-
1611
### [Example 1]
1712
**Input**: `nums = [2,7,11,15], target = 9`
1813

@@ -31,9 +26,23 @@ Difficulty: **Easy**
3126
- `-10**9 <= target <= 10**9`
3227
- **Only one valid answer exists.**
3328

34-
## Solution 1: Two pointers (should master)
35-
[中文题解](#中文题解)
29+
### [Hints]
30+
<details>
31+
<summary>Hint 1</summary>
32+
A really brute force way would be to search for all possible pairs of numbers but that would be too slow. Again, it's best to try out brute force solutions for just for completeness. It is from these brute force solutions that you can come up with optimizations.
33+
</details>
34+
35+
<details>
36+
<summary>Hint 2</summary>
37+
So, if we fix one of the numbers, say `x`, we have to scan the entire array to find the next number `y` which is `value - x` where value is the input parameter. Can we change our array somehow so that this search becomes faster?
38+
</details>
3639

40+
<details>
41+
<summary>Hint 3</summary>
42+
The second train of thought is, without changing the array, can we use additional space somehow? Like maybe a hash map to speed up the search?
43+
</details>
44+
45+
## Intuition
3746
1. The time complexity of the brute force solution is `O(n**2)`. To improve efficiency, you can sort the array, and then use **two pointers**, one pointing to the head of the array and the other pointing to the tail of the array, and decide `left += 1` or `right -= 1` according to the comparison of `sum` and `target`.
3847
2. After finding the two values which `sum` is `target`, you can use the `index()` method to find the `index` corresponding to the value.
3948

@@ -252,75 +261,7 @@ def two_sum(nums, target)
252261
end
253262
```
254263

255-
## C
256-
```c
257-
// Welcome to create a PR to complete the code of this language, thanks!
258-
```
259-
260-
## Kotlin
261-
```kotlin
262-
// Welcome to create a PR to complete the code of this language, thanks!
263-
```
264-
265-
## Swift
266-
```swift
267-
// Welcome to create a PR to complete the code of this language, thanks!
264+
## C, Kotlin, Swift, Rust or other languages
268265
```
269-
270-
## Rust
271-
```rust
272266
// Welcome to create a PR to complete the code of this language, thanks!
273267
```
274-
275-
## Other languages
276-
```
277-
// Welcome to create a PR to complete the code of this language, thanks!
278-
```
279-
280-
## 问题描述
281-
给定一个整数数组 `nums` 和一个整数目标值 `target`,请你在该数组中找出 **和为目标值** `target` 的那 **两个** 整数,并返回它们的数组下标。
282-
283-
你可以假设每种输入只会对应一个答案,并且你不能使用两次相同的元素。
284-
285-
你可以按任意顺序返回答案。
286-
287-
难度: **容易**
288-
289-
### [示例 1]
290-
**输入**: `nums = [2,7,11,15], target = 9`
291-
292-
**输出**: `[0,1]`
293-
294-
**解释**: `Because nums[0] + nums[1] == 9, we return [0, 1].`
295-
296-
# 中文题解
297-
## 思路1:双指针
298-
1. 暴力解法的时间复杂度为`O(n**2)`,想提升效率,可以对数组进行排序,然后用双指针,一个指向数组头,一个指向数组尾,根据****情况决定`left += 1`还是`right -= 1`
299-
2. 找出了两个值后,需要用`index()`方法去找值对应的`index`
300-
301-
## 思路2:使用Map提升查找一个值的效率
302-
1. `Map`中,`key``num``value`是数组`index`
303-
2. 遍历数组,如果`target - num``Map`中,返回。反之,将`num`加入`Map`中。
304-
305-
### 步骤
306-
1. `Map`中,`key``num``value`是数组`index`
307-
```javascript
308-
let numToIndex = new Map()
309-
310-
for (let i = 0; i < nums.length; i++) {
311-
numToIndex.set(nums[i], i)
312-
}
313-
```
314-
315-
2. 遍历数组,如果`target - num``Map`中,返回。反之,将`num`加入`Map`中。
316-
```javascript
317-
let numToIndex = new Map()
318-
319-
for (let i = 0; i < nums.length; i++) {
320-
if (numToIndex.has(target - nums[i])) { // 1
321-
return [numToIndex.get(target - nums[i]), i] // 2
322-
}
323-
324-
numToIndex.set(nums[i], i)
325-
}
326-
```

en/3001-4000/unorganized.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,11 @@ The improved way with a queue is commonly more efficient. Relaxing **All Edges**
6363
* Find all the prime numbers within 1000000.
6464

6565
### Skipped problems/solutions
66-
- 383 https://leetcode.cn/problems/ransom-note/
66+
- 1047 https://leetcode.cn/problems/remove-all-adjacent-duplicates-in-string/
67+
- 150 https://leetcode.cn/problems/evaluate-reverse-polish-notation/
68+
- 239 https://leetcode.cn/problems/sliding-window-maximum/ tag `monotonic queue`
69+
- 347 https://leetcode.cn/problems/top-k-frequent-elements/ tag `heap sort`
6770
-
68-
6971
# Features
7072
* 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
7173

zh/1-1000/1-two-sum.md

Lines changed: 49 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,62 @@
1-
# 1. Two Sum - LeetCode Solution
2-
LeetCode problem link: [1. Two Sum](https://leetcode.com/problems/two-sum),
3-
[1. 两数之和](https://leetcode.cn/problems/two-sum)
1+
# 1. 两数之和 - 力扣题解最佳实践
2+
力扣链接:[1. 两数之和](https://leetcode.cn/problems/two-sum), 难度: **简单**
43

5-
[中文题解](#中文题解)
4+
## 力扣“1. 两数之和”问题描述
5+
给定一个整数数组 `nums` 和一个整数目标值 `target`,请你在该数组中找出 **和为目标值** `target` 的那 **两个** 整数,并返回它们的数组下标。
66

7-
## LeetCode problem description
8-
Given an array of integers `nums` and an integer `target`, return _indices of the two numbers such that they add up to `target`_.
7+
你可以假设每种输入只会对应一个答案,并且你不能使用两次相同的元素。
98

10-
You may assume that each input would have **_exactly_ one solution**, and you may not use the same element twice.
9+
你可以按任意顺序返回答案。
1110

12-
You can return the answer in any order.
11+
### [示例 1]
12+
**输入**: `nums = [2,7,11,15], target = 9`
1313

14-
Difficulty: **Easy**
14+
**输出**: `[0,1]`
1515

16-
### [Example 1]
17-
**Input**: `nums = [2,7,11,15], target = 9`
16+
**解释**: `因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。`
1817

19-
**Output**: `[0,1]`
18+
### [示例 2]
19+
**输入**: `nums = [3,2,4], target = 6`
2020

21-
**Explanation**`Because nums[0] + nums[1] == 9, we return [0, 1].`
21+
**输出**: `[1,2]`
2222

23-
### [Example 2]
24-
**Input**: `nums = [3,2,4], target = 6`
23+
### [示例 3]
24+
**输入**: `nums = [3,3], target = 6`
2525

26-
**Output**: `[1,2]`
26+
**输出**: `[0,1]`
2727

28-
### [Constraints]
29-
- `2 <= nums.length <= 10**4`
28+
### [约束]
29+
- `2 <= nums.length <= 10000`
3030
- `-10**9 <= nums[i] <= 10**9`
31-
- `-10**9 <= target <= 10**9`
32-
- **Only one valid answer exists.**
33-
34-
## Solution 1: Two pointers (should master)
35-
[中文题解](#中文题解)
36-
37-
1. The time complexity of the brute force solution is `O(n**2)`. To improve efficiency, you can sort the array, and then use **two pointers**, one pointing to the head of the array and the other pointing to the tail of the array, and decide `left += 1` or `right -= 1` according to the comparison of `sum` and `target`.
38-
2. After finding the two values which `sum` is `target`, you can use the `index()` method to find the `index` corresponding to the value.
39-
40-
### Complexity
41-
* Time: `O(N * log N)`.
42-
* Space: `O(N)`.
31+
- **只会存在一个有效答案**
32+
33+
### [提示]
34+
<details>
35+
<summary>提示 1</summary>
36+
A really brute force way would be to search for all possible pairs of numbers but that would be too slow. Again, it's best to try out brute force solutions for just for completeness. It is from these brute force solutions that you can come up with optimizations.
37+
</details>
38+
39+
<details>
40+
<summary>提示 2</summary>
41+
So, if we fix one of the numbers, say `x`, we have to scan the entire array to find the next number `y` which is `value - x` where value is the input parameter. Can we change our array somehow so that this search becomes faster?
42+
</details>
43+
44+
<details>
45+
<summary>提示 3</summary>
46+
The second train of thought is, without changing the array, can we use additional space somehow? Like maybe a hash map to speed up the search?
47+
</details>
48+
49+
## 思路
50+
### 思路1:双指针
51+
1. 暴力解法的时间复杂度为`O(n**2)`,想提升效率,可以对数组进行排序,然后用双指针,一个指向数组头,一个指向数组尾,根据****情况决定`left += 1`还是`right -= 1`
52+
2. 找出了两个值后,需要用`index()`方法去找值对应的`index`
4353

44-
## Solution 2: Use Map (also should master)
45-
1. In `Map`, `key` is `num`, and `value` is array `index`.
46-
2. Traverse the array, if `target - num` is in `Map`, return it. Otherwise, add `num` to `Map`.
54+
### 思路2:使用 Map 提升查找一个值的效率
55+
1. `Map`中,`key``num``value`是数组`index`
56+
2. 遍历数组,如果`target - num``Map`中,返回。反之,将`num`加入`Map`中。
4757

48-
### Steps
49-
1. In `Map`, `key` is `num`, and `value` is array `index`.
58+
#### 步骤
59+
1. `Map`中,`key``num``value`是数组`index`
5060
```javascript
5161
let numToIndex = new Map()
5262

@@ -55,7 +65,7 @@ for (let i = 0; i < nums.length; i++) {
5565
}
5666
```
5767

58-
2. Traverse the array, if `target - num` is in `Map`, return it. Otherwise, add `num` to `Map`.
68+
2. 遍历数组,如果`target - num``Map`中,返回。反之,将`num`加入`Map`中。
5969
```javascript
6070
let numToIndex = new Map()
6171

@@ -68,9 +78,9 @@ for (let i = 0; i < nums.length; i++) {
6878
}
6979
```
7080

71-
### Complexity
72-
* Time: `O(n)`.
73-
* Space: `O(n)`.
81+
## 复杂度
82+
* 时间:`O(n)`
83+
* 空间:`O(n)`
7484

7585
## Java
7686
### Solution 1: Two pointers
@@ -276,51 +286,3 @@ end
276286
```
277287
// Welcome to create a PR to complete the code of this language, thanks!
278288
```
279-
280-
## 问题描述
281-
给定一个整数数组 `nums` 和一个整数目标值 `target`,请你在该数组中找出 **和为目标值** `target` 的那 **两个** 整数,并返回它们的数组下标。
282-
283-
你可以假设每种输入只会对应一个答案,并且你不能使用两次相同的元素。
284-
285-
你可以按任意顺序返回答案。
286-
287-
难度: **容易**
288-
289-
### [示例 1]
290-
**输入**: `nums = [2,7,11,15], target = 9`
291-
292-
**输出**: `[0,1]`
293-
294-
**解释**: `Because nums[0] + nums[1] == 9, we return [0, 1].`
295-
296-
# 中文题解
297-
## 思路1:双指针
298-
1. 暴力解法的时间复杂度为`O(n**2)`,想提升效率,可以对数组进行排序,然后用双指针,一个指向数组头,一个指向数组尾,根据****情况决定`left += 1`还是`right -= 1`
299-
2. 找出了两个值后,需要用`index()`方法去找值对应的`index`
300-
301-
## 思路2:使用Map提升查找一个值的效率
302-
1. `Map`中,`key``num``value`是数组`index`
303-
2. 遍历数组,如果`target - num``Map`中,返回。反之,将`num`加入`Map`中。
304-
305-
### 步骤
306-
1. `Map`中,`key``num``value`是数组`index`
307-
```javascript
308-
let numToIndex = new Map()
309-
310-
for (let i = 0; i < nums.length; i++) {
311-
numToIndex.set(nums[i], i)
312-
}
313-
```
314-
315-
2. 遍历数组,如果`target - num``Map`中,返回。反之,将`num`加入`Map`中。
316-
```javascript
317-
let numToIndex = new Map()
318-
319-
for (let i = 0; i < nums.length; i++) {
320-
if (numToIndex.has(target - nums[i])) { // 1
321-
return [numToIndex.get(target - nums[i]), i] // 2
322-
}
323-
324-
numToIndex.set(nums[i], i)
325-
}
326-
```

0 commit comments

Comments
 (0)