Skip to content

Commit a470714

Browse files
committed
Perfected 1-two-sum.md.
1 parent 307293d commit a470714

2 files changed

Lines changed: 10 additions & 2 deletions

File tree

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ The second train of thought is, without changing the array, can we use additiona
4444

4545
## Intuition
4646
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-
2. After finding the two values which `sum` is `target`, you can use the `index()` method to find the `index` corresponding to the value.
47+
48+
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+
50+
- Solution 1: Use `index()` method to find it.
51+
- Solution 2: Bring the `index` when sorting, that is, the object to be sorted is an array of tuples of `(num, index)`.
4852

4953
### Complexity
5054
* Time: `O(N * log N)`.

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@ The second train of thought is, without changing the array, can we use additiona
4949
## 思路
5050
### 思路1:双指针
5151
1. 暴力解法的时间复杂度为`O(n**2)`,想提升效率,可以对数组进行排序,然后用双指针,一个指向数组头,一个指向数组尾,根据**和**情况决定`left += 1`还是`right -= 1`。
52-
2. 找出了两个值后,需要用`index()`方法去找值对应的`index`。
52+
53+
2. 对数值数组排序后,想知道某个数值对应的原来的索引下标,有两种方案:
54+
55+
- 方案1:使用index() 查找;
56+
- 方案2:在排序时带上索引下标,即排序的对象是元组`(num, index)`的数组。
5357

5458
### 思路2:使用 Map 提升查找一个值的效率
5559
1. `Map`中,`key`是`num`,`value`是数组`index`。

0 commit comments

Comments
 (0)