-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path338_Counting_Bits.py
More file actions
46 lines (34 loc) · 898 Bytes
/
338_Counting_Bits.py
File metadata and controls
46 lines (34 loc) · 898 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
"""
Given an integer n, return an array ans of length n + 1 such that for each i (0 <= i <= n), ans[i] is the number of 1's in the binary representation of i.
Example 1:
Input: n = 2
Output: [0,1,1]
Explanation:
0 --> 0
1 --> 1
2 --> 10
Example 2:
Input: n = 5
Output: [0,1,1,2,1,2]
Explanation:
0 --> 0 0= 0
1 --> 1 1= 1+dp[n-1]
2 --> 10 1= 1+dp[n-2]
3 --> 11 2= 1+dp[n-2]
4 --> 100 1= 1+dp[n-4]
5 --> 101 2= 1+dp[n-4]
6 --> 110 2= 1+dp[n-4]
7 --> 111 3=1+dp[n-4]
8 --> 1000 1 = 1+dp[n-8]
找出那個dp的規律之後就是寫一個簡單的公式
"""
class Solution:
def countBits(self, n: int) -> List[int]:
dp = [0] * (n+1)
offset = 1
for i in range(1,n+1):
if offset *2 == i:
offset = i
dp[i] = 1+dp[i-offset]
print(i, dp[i],offset)
return dp