-
Notifications
You must be signed in to change notification settings - Fork 1
/
167. 两数之和 II - 输入有序数组
42 lines (35 loc) · 1.14 KB
/
167. 两数之和 II - 输入有序数组
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class Solution:
def twoSum(self, numbers, target: int) :
numbers_length=len(numbers)
for i in range(numbers_length-1):
left=i+1
right=numbers_length-1
target2 = target-numbers[i]
if target2<numbers[i+1]:
return
while left<=right:
mid=(left+right)//2
if numbers[mid]>target2:
right=mid-1
elif numbers[mid]==target2:
return [i+1,mid+1]
else:
left=mid+1
# 自己写的二分查找
class Solution:
def twoSum(self, numbers: List[int], target: int) -> List[int]:
if(not numbers):
return []
res=[]
n=len(numbers)
l=0
r=n-1
while(l<r):
if(numbers[l]+numbers[r]==target):
return [l+1,r+1]
elif(numbers[l]+numbers[r]>target):
r=r-1
else:
l=l+1
return [-1,-1]
# 链接:https://leetcode-cn.com/problems/two-sum-ii-input-array-is-sorted/solution/shuang-zhi-zhen-fang-fa-he-ji-zhu-xing-jie-shi-pyt/