leetcode/0191_number-of-1-bits/python3/solution.py

10 lines
185 B
Python
Raw Normal View History

2022-04-25 21:01:01 +00:00
class Solution:
def hammingWeight(self, n: int) -> int:
count = 0
while n:
2022-04-25 21:06:21 +00:00
n = n & (n - 1)
count += 1
2022-04-25 21:01:01 +00:00
return count