Skip to content

Commit 7720eff

Browse files
author
최준영[chris][Tech]
committed
two sum refactoring
1 parent 849cf91 commit 7720eff

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

two-sum/chrisjune.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33

44
class Solution:
55
def twoSum(self, nums: List[int], target: int) -> List[int]:
6+
nums_dic = {num: idx for idx, num in enumerate(nums)}
67
for i in range(len(nums)):
7-
for j in range(len(nums)):
8-
if i == j:
9-
continue
10-
if nums[i] + nums[j] == target:
11-
return [i, j]
8+
remain = target - nums[i]
9+
exists_idx = nums_dic.get(remain)
10+
if exists_idx and exists_idx != i:
11+
return i, exists_idx

0 commit comments

Comments
 (0)