We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 04da391 commit c99944eCopy full SHA for c99944e
Dynamic Programming/Easy/Counting Bits.py
@@ -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
22
+ binary = self.convert_to_binary(i)
23
+ binary_list += [binary.count('1')]
24
+ return binary_list
25
0 commit comments