diff --git a/0191_number-of-1-bits/python3/shift.py b/0191_number-of-1-bits/python3/shift.py new file mode 100644 index 0000000..1dd8edc --- /dev/null +++ b/0191_number-of-1-bits/python3/shift.py @@ -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 diff --git a/0191_number-of-1-bits/python3/solution.py b/0191_number-of-1-bits/python3/solution.py index 1dd8edc..02dafcb 100644 --- a/0191_number-of-1-bits/python3/solution.py +++ b/0191_number-of-1-bits/python3/solution.py @@ -3,7 +3,7 @@ class Solution: count = 0 while n: - count += 1 if n & 1 == 1 else 0 - n >>= 1 + n = n & (n - 1) + count += 1 return count