Number of 1 Bits

Given a positive integer n, return the number of set bits in its binary representation, also known as the Hamming weight.

Constraints

  • 1 <= n <= 2^31 - 1
Saving… · 2 lines
class Solution:
def hammingWeight(self, n: int) -> int:
Console

Click Run to execute sample tests.