-
-
Notifications
You must be signed in to change notification settings - Fork 245
[hu6r1s] WEEK 03 Solutions #1784
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
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
78f0cd6
feat: Solve valid-palindrome problem
hu6r1s 176ddb4
feat: Solve number-of-1-bits problem
hu6r1s 1ac5131
feat: Solve combination-sum problem
hu6r1s 1c32cb4
feat: Solve maximum-subarray problem
hu6r1s 8cb65c8
feat: Solve decode-ways problem
hu6r1s File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
class Solution: | ||
""" | ||
1. n이 11일때, 이진수로 변환해보면 1011이다. set bits는 1의 값을 찾는 것이기에 1을 카운트해준다. | ||
시간 복잡도 (Time Complexity): | ||
- bin(n): 정수를 이진 문자열로 변환 → O(log n) | ||
(n의 크기에 따라 필요한 비트 수만큼 연산함) | ||
- count('1'): 문자열에서 '1'의 개수를 세기 위해 전체 순회 → O(log n) | ||
(이진 문자열의 길이는 log₂(n)에 비례) | ||
최종 시간 복잡도: O(log n) | ||
|
||
공간 복잡도 (Space Complexity): | ||
- bin(n)의 결과로 생성된 이진 문자열을 저장 → O(log n) | ||
- 그 외 별도의 추가 공간 없음 | ||
최종 공간 복잡도: O(log n) | ||
""" | ||
def hammingWeight(self, n: int) -> int: | ||
return bin(n).count('1') | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
bin()이 문자열을 새로 생성하기 때문에 매우 큰 수가 연속적으로 많이 호출되면 약간의 오버헤드가 있을 수 있음 참고