题目
题解
思路:首尾指针向内收缩,比target大,就收缩尾指针,比target小,就收缩首指针,等于就返回;
class Solution:
def twoSum(self, numbers: List[int], target: int) -> List[int]:
nums = list()
x, y = 0, len(numbers)-1
while x < y:
if numbers[x] + numbers[y] > target:
y -= 1
if numbers[x] + numbers[y] < target:
x += 1
if numbers[x] + numbers[y] == target:
nums = [x+1, y+1]
break
return nums