Skip to content

Commit 7fc5e2b

Browse files
committed
feat: finish two sum in python
1 parent 2abad2f commit 7fc5e2b

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

alternative/easy/two_sum.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
def two_sum(nums, target):
2+
map = {}
3+
4+
for i, num in enumerate(nums):
5+
complement = target - num
6+
7+
if complement in map:
8+
return [map[complement], i]
9+
10+
map[num] = i
11+
12+
return []
13+
14+
# Test cases
15+
assert two_sum([2, 7, 11, 15], 9) == [0, 1]
16+
assert two_sum([3, 2, 4], 6) == [1, 2]
17+
assert two_sum([3, 3], 6) == [0, 1]

0 commit comments

Comments
 (0)