-
-
Notifications
You must be signed in to change notification settings - Fork 248
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
Saysimple week4 #95
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
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] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# TC: O(n), SC: O(1) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
class Solution: | ||
def missingNumber(self, nums: List[int]) -> int: | ||
for i in range(len(nums)+1): | ||
if i not in nums: | ||
return i |
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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. count 메서드를 쓰니 한줄 코드가 되는군요! |
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 혹시 코딩 테스트에서도 이렇게 간결하게 작성하는게 유리할까요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이래서 제가 비트 조작 문제는 실제 시험에 잘 안 나온다고 한 겁니다 ㅋㅋㅋ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
저는 약간 복잡하게 풀었는데 이 풀이 보니 엄청 직관적이네요! 배우고 갑니다.