Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def twoSum(self, nums: List[int], target: int) -> List[int]:
- """ Sort the array and then work inwards from the outer ends:
- constant memory and nlogn time.
- Use a dict to keep track of seen numbers, and if you find a match,
- return that: constant time and o(n) memory.
- """
- seen_nums = dict()
- for i, num in enumerate(nums):
- difference = target - num
- if difference in seen_nums:
- return seen_nums[difference], i
- seen_nums[num] = i
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement