-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwoSum.py
More file actions
32 lines (25 loc) · 794 Bytes
/
Copy pathtwoSum.py
File metadata and controls
32 lines (25 loc) · 794 Bytes
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
# def twoSum(nums, target):
# countNumber = 0
# arrPlace = []
# for index, value in enumerate(nums):
# addTwoNumber = countNumber + value
# countNumber = value
# if len(arrPlace) > 1:
# arrPlace[0] = arrPlace[1]
# arrPlace[1] = index
# else:
# arrPlace.append(index)
# if addTwoNumber == target:
# return arrPlace
# twoSum([2,7,11,15], 9)
# The Solution
def twoSum(nums, target):
index_map = {}
for index, value in enumerate(nums):
complement = target - value
if complement in index_map:
return [index_map[complement], index]
index_map[value] = index
return []
result = twoSum([2,7,11,15], 9)
print(result)