思路:双指针。
1 class Solution(object): 2 def twoSum(self, numbers, target): 3 """ 4 :type numbers: List[int] 5 :type target: int 6 :rtype: List[int] 7 """ 8 index1, index2 = 0, len(numbers) - 1 9 while index1 < index2: 10 if numbers[index1] + numbers[index2] == target: 11 return [index1 + 1, index2 + 1] 12 elif numbers[index1] + numbers[index2] > target: 13 index2 -= 1 14 else: 15 index1 += 1 16 17 18 if __name__ == '__main__': 19 solution = Solution() 20 print(solution.twoSum(numbers=[2, 7, 11, 15], target=9))