Skip to content

Commit 4b16780

Browse files
authored
Create two_sum.py
1 parent 309cb69 commit 4b16780

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

0001/two_sum.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution(object):
2+
def twoSum(self, nums, target):
3+
"""
4+
:type nums: List[int]
5+
:type target: int
6+
:rtype: List[int]
7+
"""
8+
hash_table = dict()
9+
10+
# Iterating the list
11+
for i, num in enumerate(nums):
12+
13+
# If the target-num in dict then, we found the solution
14+
try:
15+
hash_table[target - num]
16+
return [hash_table[target - num], i]
17+
except KeyError:
18+
hash_table[num] = i

0 commit comments

Comments
 (0)