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
+47-36Lines changed: 47 additions & 36 deletions
Original file line number
Diff line number
Diff line change
@@ -18,7 +18,9 @@ You can return the answer in any order.
18
18
19
19
**Output**:`[0,1]`
20
20
21
-
**Explanation**: Because nums[0] + nums[1] == 9, we return[0, 1].
21
+
**Explanation**:
22
+
23
+
Because nums[0] + nums[1] == 9, we return[0, 1].
22
24
23
25
###[Example 2]
24
26
@@ -41,30 +43,37 @@ You can return the answer in any order.
41
43
42
44
###[Hints]
43
45
44
-
Hint 1
45
-
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.
47
-
48
-
---
46
+
<details>
47
+
<summary>Hint 1</summary>
48
+
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.
49
49
50
-
Hint 2
50
+
51
+
</details>
51
52
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?
53
+
<details>
54
+
<summary>Hint 2</summary>
55
+
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?
53
56
54
-
---
57
+
58
+
</details>
55
59
56
-
Hint 3
60
+
<details>
61
+
<summary>Hint 3</summary>
62
+
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?
57
63
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?
64
+
65
+
</details>
59
66
60
67
##Intuition 1
61
68
62
69
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`.
63
70
64
71
2. After sorting an array of numbers, if you want to know the original`index` corresponding to a certain value, there are two solutions:
65
72
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()` method to find it. I have discussed this in another solution.
73
+
<details><summary>Click to view the answer</summary><p>
74
+
- 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.
75
+
- Solution 2: Use `index()` method to find it. I have discussed this in another solution.
vector<int> twoSum(vector<int>& nums, int target) {
177
189
unordered_map<int, int> num_to_index;
178
-
190
+
179
191
for (auto i = 0; i < nums.size(); i++) {
180
192
if (num_to_index.contains(target - nums[i])) {
181
193
return {num_to_index[target - nums[i]], i};
@@ -194,7 +206,7 @@ public:
194
206
```javascript
195
207
var twoSum = function (nums, target) {
196
208
let numToIndex = new Map()
197
-
209
+
198
210
for (let i = 0; i < nums.length; i++) {
199
211
if (numToIndex.has(target - nums[i])) {
200
212
return [numToIndex.get(target - nums[i]), i]
@@ -211,7 +223,7 @@ var twoSum = function (nums, target) {
211
223
public class Solution {
212
224
public int[] TwoSum(int[] nums, int target) {
213
225
var numToIndex = new Dictionary<int, int>();
214
-
226
+
215
227
for (int i = 0; i < nums.Length; i++) {
216
228
if (numToIndex.ContainsKey(target - nums[i])) {
217
229
return [numToIndex[target - nums[i]], i];
@@ -268,7 +280,6 @@ end
268
280
## Intuition3
269
281
270
282
1. The time complexityof 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 headof the array and the other pointing to the tailof the array, and decide`left += 1` or`right -= 1` according to the comparisonof`sum` and`target`.
271
-
272
283
2. After finding the two values which`sum` is`target`, you can use the`index()` method to find the`index` corresponding to the value.