Skip to content

Commit 15182a4

Browse files
authored
Merge pull request #2455 from ppxyn1/main
[ppxyn1] WEEK 03 solutions
2 parents 33dc6e9 + 6305af0 commit 15182a4

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

number-of-1-bits/ppxyn1.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# idea: -
22

3+
# Ans 1
4+
# Time Complexity: O(N) ?
35
from collections import Counter
46
class Solution:
57
def hammingWeight(self, n: int) -> int:
@@ -8,3 +10,13 @@ def hammingWeight(self, n: int) -> int:
810

911

1012

13+
# Ans 2
14+
# Time Complexity: O(N)
15+
class Solution:
16+
def hammingWeight(self, n: int) -> int:
17+
cnt = 0
18+
while n:
19+
cnt += n & 1 # check current bit
20+
n >>= 1 # move to next bit
21+
return cnt
22+

0 commit comments

Comments
 (0)