167 - Two Sum II (Input Array Is Sorted)
Details
Key | Value |
---|---|
Link | https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/ |
Language | Python 3 |
Runtime | 176 ms, faster than 50.98% of Python3 online submissions for Two Sum II - Input Array Is Sorted |
Memory Usage | 14.8 MB, less than 89.05% of Python3 online submissions for Two Sum II - Input Array Is Sorted |
Datastructures | List[int] |
Algorithms | Two Pointers |
Procedure
- Left pointer starts at first index Right pointer starts ar last index
- Until the two pointers meet in the middle...
- If sum of left and right matches the target we're done
- If sum of left and right is less, increase left pointer
- If sum of left and right is more, decrease right pointer
Code
class Solution:
def twoSum(self, numbers: List[int], target: int) -> List[int]:
left, right = 0, len(numbers) - 1
while left < right:
if numbers[left] + numbers[right] == target:
return [left+1, right+1]
elif numbers[left] + numbers[right] < target:
left += 1
else:
right -= 1