LeetCode/Grind_70/TwoSum.py

9 lines
358 B
Python

def twoSum(nums, target):
index = {} # create a dictionary
for i in range(len(nums)):
if (target-nums[i]) in index: # pass each value until its' matching pair is found
return [index[target-nums[i]], i]
index[nums[i]] = i # store values of non-matching pair
print(twoSum([2,7,11,15], 9))
# print(twoSum([3,3], 6))