#191
Number of 1 Bits
easy· Bit Manipulationruns: 0Write a function that takes the binary representation of a positive integer and returns the number of set bits it has (also known as the Hamming weight).
sign in to paste and practice your own solution
wpm 0acc 100%time 0:000 / 162
class Solution:
def hammingWeight(self, n: int) -> int:
count = 0
while n:
n &= n - 1
count += 1
return count
click the box to focus · tab inserts 4 spaces · backspace to correct · esc to pause
desktop only
codedrill is a typing game and needs a real keyboard. open this on a laptop or desktop to practice.
you can still browse problems and sections from your phone.