Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def twoSum(self, numbers: List[int], target: int) -> List[int]:
- """
- [2,7,11,15], target = 18
- Step 1: 17, we need more, so step in from the left
- Step 2: 22, we need less, so step in from the right
- Proposed solution: We start with the two pointers at the ends and
- step inwards based on whether the current sum is greater or less
- than the target sum.
- """
- l = 0
- r = len(numbers) - 1
- while l < r: # TODO: Double-check this condition.
- current_sum = numbers[l] + numbers[r]
- if current_sum == target:
- return [l+1, r+1]
- elif current_sum < target:
- l += 1
- else:
- r -= 1
Advertisement
Add Comment
Please, Sign In to add comment