-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtwo_sum.py
45 lines (31 loc) · 1.14 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
43
44
45
#!/usr/bin/env python3
"""Two Sums.
Given an array of integers, return indices of the two
numbers such that they add up to a specific target.
You may assume that each input would have exactly
one solution, and you may not use the same element twice.
https://leetcode.com/problems/two-sum/description/
"""
def two_sums_brute_force(nums: list, target: int, *args, **kwargs):
"""Return the two indices in nums that add up to target.
This is a Brute force method.
"""
for i in nums:
for j in nums[1:]:
if i + j == target:
return f'[{nums.index(i)}, {nums.index(j)}]'
def two_sums(nums: list, target: int, *args, **kwargs):
"""Return the two indices in nums that add up to target.
This uses python dictionaries.
"""
map_ = dict(zip(nums, range(len(nums))))
for key, value in map_.items():
complement = target - key
if complement in map_.keys():
return f'[{value}, {map_[complement]}]'
return f'No two sum solution'
if __name__ == "__main__":
nums = [2, 7, 11, 15]
target = 9
print(two_sums_brute_force(**locals()))
print(two_sums(**locals()))