Skip to content

Commit e50abed

Browse files
authored
Update 167._two_sum_ii_-_input_array_is_sorted.md
1 parent 3c7b3a0 commit e50abed

File tree

1 file changed

+29
-11
lines changed

1 file changed

+29
-11
lines changed
Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,38 @@
1-
### 167. Two Sum II - Input array is sorted
1+
# 167. Two Sum II - Input array is sorted
22

3+
**<font color=red>难度: Easy</font>**
34

5+
## 刷题内容
46

5-
题目:
6-
<https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/>
7+
> 原题连接
78
9+
* https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/description/
810

9-
难度:
10-
Medium
11+
> 内容描述
1112
13+
```
14+
15+
Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.
16+
17+
The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2.
18+
19+
Note:
20+
21+
Your returned answers (both index1 and index2) are not zero-based.
22+
You may assume that each input would have exactly one solution and you may not use the same element twice.
23+
Example:
24+
25+
Input: numbers = [2,7,11,15], target = 9
26+
Output: [1,2]
27+
Explanation: The sum of 2 and 7 is 9. Therefore index1 = 1, index2 = 2.
28+
```
1229

13-
思路:
30+
## 解题方案
1431

32+
> 思路 1
33+
******- 时间复杂度: O(lgN)******- 空间复杂度: O(1)******
1534

16-
双指针
35+
二分法+双指针,beats 86.15%
1736

1837
```python
1938
class Solution(object):
@@ -27,9 +46,8 @@ class Solution(object):
2746
while l < r:
2847
if numbers[l] + numbers[r] == target:
2948
return [l+1, r+1]
30-
elif numbers[l] + numbers[r] > target:
31-
r -= 1
32-
else:
49+
elif numbers[l] + numbers[r] < target:
3350
l += 1
34-
51+
else:
52+
r -= 1
3553
```

0 commit comments

Comments
 (0)