-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path338.py
34 lines (30 loc) · 782 Bytes
/
338.py
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
class Solution(object):
dp = [0,1,1]
def pow2last(self):
len_dp = len(self.dp)
count = 1
while count < len_dp:
count <<= 1
return count >> 1
def countBits(self, num):
"""
:type num: int
:rtype: List[int]
"""
if 1+num < len(self.dp):
return self.dp[:num+1]
last = self.pow2last()
now = last << 1
i = len(self.dp)
while i <= num:
if i == now:
self.dp.append(1)
last = now
now = last << 1
else:
self.dp.append(self.dp[i-last]+1)
i += 1
return self.dp[:num+1]
if __name__ == '__main__':
wds = Solution()
print wds.countBits(5)