Two Sum Problem

https://leetcode.com/problems/two-sum/

  • Solution 1 - brute force
  • Solution 2 - hash table
    • Maintain a dictionary of num: index
    • Calculate the complement of the current number
    • Find the complement in the dictionary
  • Solution 3 - with two pointer.
n = len(nums)
num_map = {}
for i in range(n):
    complement = target - nums[i]
    if complement in num_map:
        return (num_map[complement])
    num_map[nums[i] = i]
return None