Skip to content

Files

Latest commit

 

History

History
23 lines (15 loc) · 578 Bytes

191. Number of 1 Bits.md

File metadata and controls

23 lines (15 loc) · 578 Bytes

Code for '191. Number of 1 Bits ' (Python)

Write a function that takes the binary representation of an unsigned integer and returns the number of '1' bits it has (also known as the Hamming weight).

Example 1:

Input: n = 00000000000000000000000000001011
Output: 3
Explanation: The input binary string 00000000000000000000000000001011 has a total of three '1' bits.
class Solution:
    def hammingWeight(self, n: int) -> int:
        c = 0
        

        while n != 0:
            n &= (n - 1)
            c += 1
        return c