Skip to content

Commit b465ae7

Browse files
committed
Number of 1 Bits
1 parent 57720ac commit b465ae7

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
""" https://leetcode.com/problems/number-of-1-bits/description/ """
2+
3+
class Solution:
4+
def hammingWeight(self, n):
5+
binary = "" # Backwards
6+
count = 0
7+
8+
while (n > 0):
9+
binary += str(n % 2)
10+
n //= 2
11+
12+
for i in binary:
13+
if (i == "1"):
14+
count += 1
15+
16+
return count
17+
18+
solution = Solution()
19+
print(solution.hammingWeight(128))

0 commit comments

Comments
 (0)