Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions two-sum/jiunshinn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# time complexity : o(n)
# space complexity : o(n)
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
hashmap = {}

for i, n in enumerate(nums):
diff = target - n
if diff in hashmap:
return [i, hashmap[diff]]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

리턴 시 인덱스 순서가 문제 요구사항과 맞지 않을 수 있으므로 return [i, hashmap[diff]] 대신 return [hashmap[diff], i]로 수정하는 것이 안전합니다.

else:
hashmap[n] = i