-
Notifications
You must be signed in to change notification settings - Fork 0
/
two_sum.py
38 lines (30 loc) · 1001 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
37
38
class Slow_solution:
def twoSum(self, nums, target):
print(f"The range is: ", len(nums))
for num in range(len(nums)):
print(f"first: ", num)
for num2 in range(num + 1, len(nums)):
print(f"second: ", num2)
answer = nums[num] + nums[num2]
if answer == target:
print("success")
return [num,num2]
else:
print("error")
class Fast_solution:
def two_sum(self,nums,target):
table={}
for index,num in enumerate(nums):
poop = target-num
if poop in table:
print(table)
return [index,table[poop]]
else:
table[num]=index
return []
nums = [8,2,4,5]
target = 9
#brute = Slow_solution()
#brute.twoSum(nums, target)
nice = Fast_solution(
nice.two_sum(nums, target)