LeetCode 167. Two Sum II - Input array is sorted

题目

LeetCode 167. Two Sum II - Input array is sorted_第1张图片

代码

class Solution:
    def twoSum(self, numbers, target):
        """
        :type numbers: List[int]
        :type target: int
        :rtype: List[int]
        """
        left = 0; right = len(numbers) - 1
        while left < right:
            num = numbers[left] + numbers[right]
            if num > target:
                right -= 1
            elif num < target:
                left += 1
            else: return [left + 1, right + 1]

你可能感兴趣的:(OJ-LeetCode,算法-贪心)