-
-
Notifications
You must be signed in to change notification settings - Fork 245
[jongwanra] WEEK 03 solutions #1793
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
3 commits
Select commit
Hold shift + click to select a range
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,45 @@ | ||
""" | ||
[Problem] | ||
https://leetcode.com/problems/number-of-1-bits/description/ | ||
양수 n이 주어졌을 때, 이진법에서 1로 설정된 비트의 개수를 반환하는 함수를 작성해라. | ||
[Plan] | ||
1. 주어진 양수를 이진수로 변환한다. | ||
2. for-loop을 순회하며 1의 개수를 counting한다. | ||
[Complexity] | ||
N: bin(n).length - 2 | ||
Time: O(N) | ||
Space = O(N) | ||
""" | ||
class Solution: | ||
def hammingWeight(self, n: int) -> int: | ||
binary = bin(n) | ||
output = 0 | ||
for index in range(2, len(binary)): | ||
if binary[index] == '1': | ||
output += 1 | ||
return output | ||
""" | ||
ref: https://www.algodale.com/problems/number-of-1-bits/ | ||
[Complexity] | ||
Time: O(log n) | ||
Space: O(1) | ||
""" | ||
class AnotherSolution: | ||
def hammingWeight(self, n: int) -> int: | ||
count = 0 | ||
while n: | ||
quotient, remainder = divmod(n, 2) | ||
print(f"n = {n} quotient={quotient}, remainder={remainder}") | ||
count += remainder | ||
n = quotient | ||
return count | ||
|
||
sol = AnotherSolution() | ||
print(sol.hammingWeight(11) == 3) | ||
print(sol.hammingWeight(128) == 1) | ||
print(sol.hammingWeight(2147483645) == 30) | ||
|
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.
Uh oh!
There was an error while loading. Please reload this page.