nathanwailes

LeetCode 167 - Two Sum II - NeetCode solution

Oct 7th, 2023
1,087
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.40 KB | None | 0 0
  1. class Solution:
  2.     def twoSum(self, numbers: List[int], target: int) -> List[int]:
  3.         l, r = 0, len(numbers) - 1
  4.        
  5.         while l < r:
  6.             curSum = numbers[l] + numbers[r]
  7.            
  8.             if curSum > target:
  9.                 r -= 1
  10.             elif curSum < target:
  11.                 l += 1
  12.             else:
  13.                 return [l + 1, r + 1]
  14.         return []
Advertisement
Add Comment
Please, Sign In to add comment