167. Two Sum II - Input array is sorted

Array 中找 pair,很显然的双指针。

class Solution:
    def twoSum(self, numbers: List[int], target: int) -> List[int]:
        l, r = 0, len(numbers)-1
        while l<r:
            cur = numbers[l] + numbers[r]
            if cur == target: return [l+1, r+1]
            if cur > target: r-=1
            elif cur < target: l+=1

Last updated

Was this helpful?