You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: en/1-1000/1-two-sum.md
+20-79Lines changed: 20 additions & 79 deletions
Original file line number
Diff line number
Diff 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**
4
3
5
-
[中文题解](#中文题解)
6
-
7
-
## LeetCode problem description
4
+
## Description of "1. Two Sum"
8
5
Given an array of integers `nums` and an integer `target`, return _indices of the two numbers such that they add up to `target`_.
9
6
10
7
You may assume that each input would have **_exactly_ one solution**, and you may not use the same element twice.
11
8
12
9
You can return the answer in any order.
13
10
14
-
Difficulty: **Easy**
15
-
16
11
### [Example 1]
17
12
**Input**: `nums = [2,7,11,15], target = 9`
18
13
@@ -31,9 +26,23 @@ Difficulty: **Easy**
31
26
-`-10**9 <= target <= 10**9`
32
27
-**Only one valid answer exists.**
33
28
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>
36
39
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
37
46
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
47
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
48
@@ -252,75 +261,7 @@ def two_sum(nums, target)
252
261
end
253
262
```
254
263
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
268
265
```
269
-
270
-
## Rust
271
-
```rust
272
266
// Welcome to create a PR to complete the code of this language, thanks!
273
267
```
274
-
275
-
## Other languages
276
-
```
277
-
// Welcome to create a PR to complete the code of this language, thanks!
- 239 https://leetcode.cn/problems/sliding-window-maximum/ tag `monotonic queue`
69
+
- 347 https://leetcode.cn/problems/top-k-frequent-elements/ tag `heap sort`
67
70
-
68
-
69
71
# Features
70
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
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?
0 commit comments