Skip to content

Commit 89b1809

Browse files
committed
Solve two-sum with python
1 parent daa942f commit 89b1809

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

two-sum/bemelon.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
# Space complexity: O(n)
3+
# Time complexity: O(n)
4+
def twoSum(self, nums: list[int], target: int) -> list[int]:
5+
num_index = {}
6+
for curr, num in enumerate(nums):
7+
rest = target - num
8+
if rest in num_index:
9+
return [num_index[rest], curr]
10+
else:
11+
num_index[num] = curr
12+
return [0, 0]
13+

0 commit comments

Comments
 (0)