We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent e6d4e0d commit f189d0aCopy full SHA for f189d0a
number-of-1-bits/doh6077.py
@@ -1,4 +1,7 @@
1
class Solution:
2
def hammingWeight(self, n: int) -> int:
3
- binary_num = format(n, 'b')
4
- return binary_num.count('1')
+ ans = 0
+ while n != 0:
5
+ ans += 1
6
+ n = n & ( n-1)
7
+ return ans
0 commit comments