Skip to content

Saysimple week4 #95

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 29, 2024
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
11 changes: 11 additions & 0 deletions counting-bits/saysimple.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# TC: O(n), SC: O(n)

class Solution:
def countBits(self, n: int) -> List[int]:
answer = []

for i in range(n+1):
b = bin(i)
answer.append(int(b.count("1")))

return answer
14 changes: 14 additions & 0 deletions group-anagrams/saysimple.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# TC: O(n), SC: O(n)

from collections import defaultdict

class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
d = defaultdict(list)
for s in strs:
if not s:
d["0"].append("")
else:
d["".join(sorted(s))].append(s)

return [d[s] for s in d]
7 changes: 7 additions & 0 deletions missing-number/saysimple.py
Copy link
Contributor

Choose a reason for hiding this comment

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

저는 약간 복잡하게 풀었는데 이 풀이 보니 엄청 직관적이네요! 배우고 갑니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# TC: O(n), SC: O(1)
Copy link
Member

@DaleSeo DaleSeo May 28, 2024

Choose a reason for hiding this comment

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

i not in nums에서 걸리는 시간을 과소 평가하신 게 아닐까요?


class Solution:
def missingNumber(self, nums: List[int]) -> int:
for i in range(len(nums)+1):
if i not in nums:
return i
5 changes: 5 additions & 0 deletions number-of-1-bits/saysimple.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# TC: O(1), SC: O(1)

class Solution:
def hammingWeight(self, n: int) -> int:
return bin(n).count("1")
Copy link
Contributor

Choose a reason for hiding this comment

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

count 메서드를 쓰니 한줄 코드가 되는군요!

5 changes: 5 additions & 0 deletions reverse-bits/saysimple.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# TC: O(1), SC: O(1)

class Solution:
def reverseBits(self, n: int) -> int:
return int(f"{bin(n)[2:]:0>32}"[::-1], 2)
Copy link
Contributor

Choose a reason for hiding this comment

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

혹시 코딩 테스트에서도 이렇게 간결하게 작성하는게 유리할까요?

Copy link
Member

Choose a reason for hiding this comment

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

이래서 제가 비트 조작 문제는 실제 시험에 잘 안 나온다고 한 겁니다 ㅋㅋㅋ