Intuition
- Visualize the number line, and take advantage of the array being sorted.
- The index + 1 return is a gimmick since you just return the indices you found + 1.
Code
class Solution:
def twoSum(self, numbers: List[int], target: int) -> List[int]:
i = 0
j = len(numbers) - 1
while i < j:
cur_sum = numbers[i] + numbers[j]
if cur_sum == target:
return [i + 1, j + 1]
elif target < cur_sum:
j -= 1
else:
i += 1References
https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/