number-of-1-bits py3 better

This commit is contained in:
Sangeeth Sudheer 2022-04-26 02:36:21 +05:30
parent 846ddad820
commit 9bf1a027c0
2 changed files with 11 additions and 2 deletions

View File

@ -0,0 +1,9 @@
class Solution:
def hammingWeight(self, n: int) -> int:
count = 0
while n:
count += 1 if n & 1 == 1 else 0
n >>= 1
return count

View File

@ -3,7 +3,7 @@ class Solution:
count = 0 count = 0
while n: while n:
count += 1 if n & 1 == 1 else 0 n = n & (n - 1)
n >>= 1 count += 1
return count return count