Skip to content

Commit 939e447

Browse files
committed
1 Two Sum.py Better Solution
1 parent 9b0fb8e commit 939e447

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

1 Two Sum.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ def twoSum(self, nums, target):
55
:type target: int
66
:rtype: List[int]
77
"""
8-
8+
dict_num = {}
99
for i in range(len(nums)):
10-
for j in range(len(nums)):
11-
if nums[i] + nums[j] == target and i != j:
12-
return [i,j]
10+
diff = target - nums[i]
11+
if diff in dict_num:
12+
return [i, dict_num[diff]]
13+
14+
dict_num[nums[i]] = i
1315

1416
return False
1517

0 commit comments

Comments
 (0)