ntree.ai
ntree.aiNumber of 1 Bits

Number of 1 Bits

easysolvedattempted

Given a non-negative integer n, return how many 1s its binary form contains.

Example 1:

Input:  n = 11
Output: 3
Explanation: 11 is 1011 in binary, which has three 1s.

Example 2:

Input:  n = 128
Output: 1
Explanation: 128 is 10000000 in binary, which has a single 1.

Constraints:

  • 0 <= n <= 2^31 - 1
def hammingWeight(n: int) -> int:
pass
 

Press Visualise to step through your code.