WillWang-X
7/18/2017 - 2:27 AM

167. Two Sum II - Input array is sorted.py

# Time: O(n)
# Space: O(1)
# 167. Two Sum II - Input array is sorted
# Edge case 

class Solution(object):
    def twoSum(self, numbers, target):
        """
        :type numbers: List[int]
        :type target: int
        :rtype: List[int]
        """
        l, r = 0, len(numbers)-1
        while l < r:
            s = numbers[l] + numbers[r]
            if s == target:
                return [l+1, r+1]
            elif s < target:
                l += 1
            else:
                r -= 1