题目描述:
题解:双指针
已知输入numbers数组按递增顺序排列。
设置指针left,初始为0,right初始为len(numbers)-1,对每组left right判断numbers[left]+numbers[right]与target的关系:
相等:直接返回
小于target:left右移
大于target:right左移
class Solution(object): def twoSum(self, numbers, target): left = 0 right = len(numbers)-1 while left<right: if numbers[left]+numbers[right]==target: return [left+1,right+1] elif numbers[left]+numbers[right]<target: left = left+1 else: right = right-1