number-of-1-bitsV1.py (257B)
1 class Solution: 2 def hammingWeight(self, n: int) -> int: 3 return count_bits(n) 4 5 6 def count_bits(n: int) -> int: 7 count = 0 8 9 while n != 0: 10 if n % 2 == 1: 11 count += 1 12 n -= 1 13 n = n // 2 14 15 return count