number-of-1-bits py3 better
This commit is contained in:
parent
846ddad820
commit
9bf1a027c0
9
0191_number-of-1-bits/python3/shift.py
Normal file
9
0191_number-of-1-bits/python3/shift.py
Normal 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
|
@ -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
|
||||||
|
Loading…
Reference in New Issue
Block a user