Skip to content

Commit 552d19a

Browse files
Two Sum
1 parent 45b18fe commit 552d19a

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

Leetcode/two_sum.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from typing import List
2+
3+
4+
def two_sum(nums: List[int], target: int) -> List[int]:
5+
n = len(nums)
6+
complement_map = {nums[0]: 0}
7+
8+
for i in range(1, n):
9+
complement = target - nums[i]
10+
11+
if complement in complement_map:
12+
return [complement_map[complement], i]
13+
else:
14+
complement_map[nums[i]] = i
15+
16+
17+
print(two_sum([2, 7, 11, 15], 9))

0 commit comments

Comments
 (0)