diff --git a/number-of-1-bits/ppxyn1.py b/number-of-1-bits/ppxyn1.py index 3762a12b3e..15a65772f3 100644 --- a/number-of-1-bits/ppxyn1.py +++ b/number-of-1-bits/ppxyn1.py @@ -1,5 +1,7 @@ # idea: - +# Ans 1 +# Time Complexity: O(N) ? from collections import Counter class Solution: def hammingWeight(self, n: int) -> int: @@ -8,3 +10,13 @@ def hammingWeight(self, n: int) -> int: +# Ans 2 +# Time Complexity: O(N) +class Solution: + def hammingWeight(self, n: int) -> int: + cnt = 0 + while n: + cnt += n & 1 # check current bit + n >>= 1 # move to next bit + return cnt +