001. two sum

Py3 - Solution

1
2
3
4
5
6
class :
def twoSum(self, nums, target):
for i in range(len(nums)):
for j in range(i+1,len(nums)):
if nums[i] + nums[j] == target:
return [i,j]
1
2
3
4
5
6
7
class :
def twoSum(self, nums, target):
dictionary = dict([(y,x) for x,y in enumerate(nums)])
for i in range(len(nums)):
x = nums[i]
if target-x in dictionary and dictionary[target-x] != i:
return (dictionary[target-x],i)
1
2
3
4
5
6
7
8
class :
def twoSum(self, nums, target):
dict = {}
for i in range(len(nums)):
x = nums[i]
if target-x in dict:
return (dict[target-x],i)
dict[x] = i