Skip to content

Commit dd4da13

Browse files
committed
#1. Two Sum
1 parent 1cf5f58 commit dd4da13

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

two_sums.py renamed to Hash_Table/two_sums.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@
3030

3131
class Solution:
3232
def twoSum(self, nums: int, target: int) -> int:
33-
for i in range(len(nums) - 1):
34-
for j in range(i+1, len(nums)):
35-
if nums[i] + nums[j] == target:
36-
return [i, j]
33+
hash_tbl = {}
34+
for i, n in enumerate(nums):
35+
c = target - n
36+
if c in hash_tbl:
37+
return [hash_tbl[c], i]
38+
hash_tbl[n] = i

0 commit comments

Comments
 (0)