Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
30 changes: 30 additions & 0 deletions contains-duplicate/jiunshinn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# 시간 복잡도 : o(n)
# 공간 복잡도 o(n)
Comment on lines +1 to +2
Copy link
Member

Choose a reason for hiding this comment

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

시간 복잡도에는 :를 쓰시고, 공간 복잡도에는 쓰시지 않는 이유가 궁금하네요?



class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
hashmap = {}

for i, n in enumerate(nums):
if n in hashmap:
return True
hashmap[n] = i
return False


# -------------------------------------------------------------------------------------------------------- #

# 시간 복잡도 : o(n)
# 공간 복잡도 o(n)


class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
seen = set()
Copy link
Member

Choose a reason for hiding this comment

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

집합을 사용하는 이 풀이가 위에 사전을 쓰는 풀이보다 더 깔끔한 것 같습니다!


for n in nums:
if n in seen:
return True
seen.add(n)
return False
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