-
Notifications
You must be signed in to change notification settings - Fork 0
/
two_sum.py
44 lines (37 loc) · 1 KB
/
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
37
38
39
40
41
42
def my_twosum(nums, target):
op = []
for i in range(len(nums)):
if(len(op) < 2):
a = target - nums[i]
j = i+1
if(a == 0):
op.append(i)
print(op[0])
quit
if(a != 0):
while(j<len(nums) and a != 0 and len(op) != 2 ):
if (nums[j] == a):
op.append(i)
op.append(j)
a += nums[i]
a = target - a
j += 1
else:
break
for k in range(len(op)):
print(op[k])
return op
def yt_twosum(nums, target):
hashmap = {}
for i, n in enumerate(nums):
if (target - n) in hashmap:
return(hashmap[target-n], i)
hashmap[n] = i
def main():
nums = [-10,-1,-18,-19]
target = -19
op = my_twosum(nums, target)
# for k in range(len(op)):
# print(op[k])
if __name__ == "__main__":
main()