题目描述:
代码:
class Solution(object):
#使用双指针
def twoSum(self, nums, target):
head,tail=0,len(nums)-1
#当尾指针小于头指针时结束
while head<=tail:
#尾指针大于目标,减小
if nums[tail]>target:
tail-=1
continue
#头指针大于目标,返回None
if nums[head]>target:
return None
#和小于目标,头指针增大
if nums[head]+nums[tail]<target:
head+=1
#和大于目标,尾指针减少
elif nums[head]+nums[tail]>target:
tail-=1
else:
return nums[head],nums[tail]