labels: Arrays, Hashing, Easy
Time Completed: 4:33 minutes
Link to problem: 1. Two Sum
- Iterate through nums
- Find difference by subtracting number from target
- See if difference can be found in hashmap
- If difference not found, insert value, index pair into hashmap. Otherwise return index of the difference found and the current index
- Know what Enumerate() does for lists and hashmaps!! It creates pairs of the value and index for lists while it creates pairs of keys and values for hashmaps.
- Was really important for this problem as it simplified getting the index of the numbers from the array
def twoSum(self, nums, target):
hashmap = {}
for index, value in enumerate(nums):
difference = target - value
if difference in hashmap:
return [hashmap[difference], index]
else:
hashmap[value] = index
return []