Skip to content

Commit c99944e

Browse files
Create Counting Bits.py
1 parent 04da391 commit c99944e

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution:
2+
3+
def countBits(self, n):
4+
dp = [0] * (n+1)
5+
for i in range(n+1):
6+
if i % 2 == 0:
7+
dp[i] = dp[i // 2]
8+
else:
9+
dp[i] = dp[i // 2] + 1
10+
return dp
11+
'''
12+
def convert_to_binary(self, num):
13+
s = ''
14+
while num >= 1:
15+
s += str(num % 2)
16+
num //= 2
17+
return s[::-1]
18+
19+
def countBits(self, n: int) -> List[int]:
20+
binary_list = []
21+
for i in range(n+1):
22+
binary = self.convert_to_binary(i)
23+
binary_list += [binary.count('1')]
24+
return binary_list
25+
'''

0 commit comments

Comments
 (0)