Number of 1 Bits LeetCode solution in Python

1
# Runtime: 27 ms (Top 98.51%) | Memory: 13.8 MB (Top 50.40%)
2
class Solution:
3
def hammingWeight(self, n: int) -> int:
4
i = 0
5
while n > 0:
6
if n % 2 != 0:
7
i += 1
8
n = n >> 1
9
return i

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0