Skip to content

Commit 2382072

Browse files
committed
number of 1 bits solution
1 parent 060958d commit 2382072

1 file changed

Lines changed: 11 additions & 0 deletions

File tree

number-of-1-bits/dolphinflow86.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# 1) Divide by 2 and count set bit.
2+
# TC: O(logN) where N is the given number.
3+
# SC: O(1)
4+
class Solution:
5+
def hammingWeight(self, n: int) -> int:
6+
setbit_count = 1
7+
8+
while n >= 2:
9+
if n % 2 == 1: setbit_count += 1
10+
n = n // 2
11+
return setbit_count

0 commit comments

Comments
 (0)