Number of 1 Bits
Solution Code
class Solution:
def hammingWeight(self, n: int) -> int:
count = 0
while n > 0:
count += n & 1
n >>= 1
return countTime Complexity
O(1)
Space Complexity
O(1)
class Solution:
def hammingWeight(self, n: int) -> int:
count = 0
while n > 0:
count += n & 1
n >>= 1
return count