In [1]:
# Input
# nums = [3,3]
nums = [2,3,5,10,5,10]
target = 10
In [2]:
# Method 1
def twoSum(nums, target):
for i in range(len(nums)):
for j in range(i+1, len(nums)):
if i != j and nums[i] + nums[j] == target:
return [i, j]
print (twoSum(nums, target))
In [3]:
# Method 2
def twoSum(nums, target):
new_dict = {}
for i, j in enumerate(nums):
remain = target - j
if remain in new_dict:
return [new_dict[remain], i]
else:
new_dict[j] = i
print (twoSum(nums, target))