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
#1. Two Sum - Best Practices of LeetCode Solutions
2
-
LeetCode link:[1. Two Sum](https://leetcode.com/problems/two-sum), difficulty:**Easy**
1
+
Original link:[leetcoder.net - LeetCoder: Fucking Good LeetCode Solutions](https://leetcoder.net/en/leetcode/1-two-sum)
2
+
3
+
#1. Two Sum - LeetCoder: Fucking Good LeetCode Solutions
4
+
5
+
LeetCode link:[1. Two Sum](https://leetcode.com/problems/two-sum), Difficulty:**Easy**.
3
6
4
7
##LeetCode description of "1. Two Sum"
8
+
5
9
Given an array of integers`nums` and an integer`target`, return*indices of the two numbers such that they add up to`target`*.
6
10
7
11
You may assume that each input would have***exactly* one solution**, and you may not use the same element twice.
8
12
9
13
You can return the answer in any order.
10
14
11
15
###[Example 1]
16
+
12
17
**Input**:`nums = [2,7,11,15], target = 9`
13
18
14
19
**Output**:`[0,1]`
15
20
16
-
**Explanation**:`Because nums[0] + nums[1] == 9, we return [0, 1].`
21
+
**Explanation**:Because nums[0] + nums[1] == 9, we return[0, 1].
17
22
18
23
###[Example 2]
24
+
19
25
**Input**:`nums = [3,2,4], target = 6`
20
26
21
27
**Output**:`[1,2]`
22
28
29
+
###[Example 3]
30
+
31
+
**Input**:`nums = [3,3], target = 6`
32
+
33
+
**Output**:`[0,1]`
34
+
23
35
###[Constraints]
36
+
24
37
-`2 <= nums.length <= 10^4`
25
38
-`-10^9 <= nums[i] <= 10^9`
26
39
-`-10^9 <= target <= 10^9`
27
40
-**Only one valid answer exists.**
28
41
29
42
###[Hints]
30
-
<details>
31
-
<summary>Hint 1</summary>
43
+
44
+
Hint 1
45
+
32
46
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
47
35
-
<details>
36
-
<summary>Hint 2</summary>
48
+
---
49
+
50
+
Hint 2
51
+
37
52
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>
39
53
40
-
<details>
41
-
<summary>Hint 3</summary>
54
+
---
55
+
56
+
Hint 3
57
+
42
58
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
59
45
-
##Intuition
60
+
##Intuition 1
61
+
46
62
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`.
47
63
48
64
2. After sorting an array of numbers, if you want to know the original`index` corresponding to a certain value, there are two solutions:
49
65
50
-
- Solution 1:Use`index()` method to find it.
51
-
- Solution 2:Bring the`index` when sorting, that is, the objecttobe sorted is an array of tuples of`(num, index)`.
66
+
- Solution 1:Bring the`index` when sorting, that is, the object to be sorted is an array of tuples of`(num, index)`. This technique**must be mastered**, as it will be used in many questions.
67
+
- Solution 2:Use`index()` methodtofind it. I have discussed this in another solution.
# Welcome to create a PR to complete the code, thanks!
253
-
```
254
247
255
-
### Solution2: UsingMap
256
248
```ruby
257
249
deftwo_sum(nums,target)
258
250
num_to_index= {}
@@ -267,7 +259,54 @@ def two_sum(nums, target)
267
259
end
268
260
```
269
261
270
-
##C, Kotlin, Swift, Rust or other languages
262
+
##Other languages
263
+
264
+
```java
265
+
// Welcome to create a PR to complete the code of this language, thanks!
271
266
```
267
+
268
+
##Intuition 3
269
+
270
+
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`.
271
+
272
+
2. After finding the two values which`sum` is`target`, you can use the`index()` method to find the`index` corresponding to the value.