1. Two Sum
Given an array of integers nums and an integer target, return the indices of the two numbers that add up to target.
nums = [2, 7, 11, 15], target = 9Output: [0, 1]
“I’ll use a hash map to track numbers I’ve seen. For each value, I’ll check whether its complement is already there.”
for i, n in enumerate(nums):
need = target - n
if need in seen:
return [seen[need], i]
seen[n] = i