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

Commit26e2838

Browse files
committed
docs: update en/1-two-sum.md to match leetcoder.net format
1 parent5da4ae8 commit26e2838

File tree

1 file changed

+47
-36
lines changed

1 file changed

+47
-36
lines changed

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

Lines changed: 47 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ You can return the answer in any order.
1818

1919
**Output**:`[0,1]`
2020

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].
2224

2325
###[Example 2]
2426

@@ -41,30 +43,37 @@ You can return the answer in any order.
4143

4244
###[Hints]
4345

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.
4949

50-
Hint 2
50+
51+
</details>
5152

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?
5356

54-
---
57+
58+
</details>
5559

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?
5763

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>
5966

6067
##Intuition 1
6168

6269
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`.
6370

6471
2. After sorting an array of numbers, if you want to know the original`index` corresponding to a certain value, there are two solutions:
6572

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.
76+
</p></details>
6877

6978
##Complexity
7079

@@ -84,7 +93,7 @@ class Solution:
8493

8594
while left< right:
8695
sum_= num_index_list[left][0]+ num_index_list[right][0]
87-
96+
8897
if sum_== target:
8998
return [num_index_list[left][1], num_index_list[right][1]]
9099

@@ -110,24 +119,27 @@ class Solution:
110119

111120
1. In`Map`,`key` is`num`, and`value` is array`index`.
112121

113-
```javascript
114-
let numToIndex=newMap()
115-
for (let i=0; i<nums.length; i++) {
116-
numToIndex.set(nums[i], i)
117-
}
118-
```
122+
```javascript
123+
let numToIndex=newMap()
124+
125+
for (let i=0; i<nums.length; i++) {
126+
numToIndex.set(nums[i], i)
127+
}
128+
```
119129

120130
2. Traverse the array,if`target - num` isin`Map`,returnit.Otherwise, add`num` to`Map`.
121131

122-
```javascript
123-
let numToIndex=newMap()
124-
for (let i=0; i<nums.length; i++) {
125-
if (numToIndex.has(target- nums[i])) {// 1
126-
return [numToIndex.get(target- nums[i]), i]// 2
127-
}
128-
numToIndex.set(nums[i], i)
129-
}
130-
```
132+
```javascript
133+
let numToIndex = new Map()
134+
135+
for (let i = 0; i < nums.length; i++) {
136+
if (numToIndex.has(target - nums[i])) { // 1
137+
return [numToIndex.get(target - nums[i]), i] // 2
138+
}
139+
140+
numToIndex.set(nums[i], i)
141+
}
142+
```
131143

132144
## Complexity
133145

@@ -140,7 +152,7 @@ for (let i = 0; i < nums.length; i++) {
140152
class Solution {
141153
public int[] twoSum(int[] nums, int target) {
142154
var numToIndex = new HashMap<Integer, Integer>();
143-
155+
144156
for (var i = 0; i < nums.length; i++) {
145157
if (numToIndex.containsKey(target - nums[i])) {
146158
return new int[]{numToIndex.get(target - nums[i]), i};
@@ -160,7 +172,7 @@ class Solution {
160172
class Solution:
161173
def twoSum(self, nums: List[int], target: int) -> List[int]:
162174
num_to_index = {}
163-
175+
164176
for i, num in enumerate(nums):
165177
if target - num in num_to_index:
166178
return [num_to_index[target - num], i]
@@ -170,12 +182,12 @@ class Solution:
170182

171183
##C++
172184

173-
```cpp
185+
```c++
174186
class Solution {
175187
public:
176188
vector<int> twoSum(vector<int>& nums, int target) {
177189
unordered_map<int, int> num_to_index;
178-
190+
179191
for (auto i = 0; i < nums.size(); i++) {
180192
if (num_to_index.contains(target - nums[i])) {
181193
return {num_to_index[target - nums[i]], i};
@@ -194,7 +206,7 @@ public:
194206
```javascript
195207
var twoSum = function (nums, target) {
196208
let numToIndex = new Map()
197-
209+
198210
for (let i = 0; i < nums.length; i++) {
199211
if (numToIndex.has(target - nums[i])) {
200212
return [numToIndex.get(target - nums[i]), i]
@@ -211,7 +223,7 @@ var twoSum = function (nums, target) {
211223
public class Solution {
212224
public int[] TwoSum(int[] nums, int target) {
213225
var numToIndex = new Dictionary<int, int>();
214-
226+
215227
for (int i = 0; i < nums.Length; i++) {
216228
if (numToIndex.ContainsKey(target - nums[i])) {
217229
return [numToIndex[target - nums[i]], i];
@@ -268,7 +280,6 @@ end
268280
## Intuition3
269281

270282
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-
272283
2. After finding the two values which`sum` is`target`, you can use the`index()` method to find the`index` corresponding to the value.
273284

274285
## Complexity

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp