Skip to content

Commit 2fc5ef0

Browse files
committed
feat: [Week 03-1] solve two-sum
1 parent 109a279 commit 2fc5ef0

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

two-sum/Chaedie.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""
2+
Solution:
3+
Use a hash map to store numbers and their indices
4+
Iterate through the list and check if difference between target and val
5+
return the index list
6+
7+
Time: O(n)
8+
Space: O(n)
9+
10+
"""
11+
12+
13+
class Solution:
14+
def twoSum(self, nums: List[int], target: int) -> List[int]:
15+
num_map = {} # num : index
16+
17+
for i, val in enumerate(nums):
18+
diff = target - val
19+
if diff in num_map:
20+
return [num_map[diff], i]
21+
num_map[val] = i

0 commit comments

Comments
 (0)