167. 两数之和 II - 输入有序数组
题目描述
解题思路
双指针,两边移动。
代码实现
class Solution:
def twoSum(self, numbers: List[int], target: int) -> List[int]:
head, tail = 0, len(numbers) - 1
while head < tail:
s = numbers[head] + numbers[tail]
if s == target:
return [head + 1, tail + 1]
elif s > target:
tail -= 1
else:
head +=1