It is a new year. Happy New Year!
On December 31, 2025, I saw a post on X (formerly Twitter) from someone who had solved LeetCode problems every day in 2025.
This has inspired me to begin my own challenge in 2026. I honestly do not know if I have the drive to see it through. I will see how it goes.
My submission for Day 1 is 191. Number of 1 Bits
This is my solution
class Solution:
def hammingWeight(self, n: int) -> int:
n_binary = ""
while n > 0:
remainder = n % 2
n = n // 2
n_binary += str(remainder)
n_binary = n_binary[::-1]
count = 0
for char in n_binary:
if char == '1':
count += 1
return count
Both time and space complexity are O(Log N).
Is there a better way to do this? Please let me know.
Top comments (0)