Skip to content

Commit 002a743

Browse files
committed
number-of-1-bits solution
1 parent e833027 commit 002a743

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

number-of-1-bits/yayyz.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# bit operation
2+
class Solution:
3+
def hammingWeight(self, n: int) -> int:
4+
count = 0
5+
while n:
6+
count += n & 1
7+
n >>= 1
8+
return count
9+
10+
# class Solution:
11+
# def hammingWeight(self, n: int) -> int:
12+
# return bin(n).count('1')

0 commit comments

Comments
 (0)