-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTwo sum.py
36 lines (18 loc) · 958 Bytes
/
Two sum.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#two sum
#return the indices of two numbers that add up to a target value
#assume that each input has exactly one solution, and the same cannot be used twice
class Solution:
def twoSum(self, nums, target): #initialize the contents of the class
self.nums=nums
self.target=target
seen={} #crete empty dictionary to store values we've seen
for i, num in enumerate(nums):
complement= target- num #calculate the complement i.e. the number needed to add up to the target
if complement in seen:
return [seen[complement], i] #return the index of the complement and the number if complement is already in seen
seen[num] =i
#otherwise, add the current number and its index to the dict
x= Solution()
nums=[2, 7, 9, 11, 15]
target= 9
print(x.twoSum(nums, target))