动态规划
class Solution:
def countBits(self, n: int) -> List[int]:
# 动态规划
# i+1 只比 i多1
bits = [0]
highbit = 0 # 最高有效位
for i in range(1, n+1):
if i & (i-1) == 0: # i-1 全为1时,更新最高有效位
highbit = i
bits.append(bits[i-highbit]+1)
# 如果是刚更新的最高有效位,则为1
# 如果不是,则下一个+1(最高有效位上的1)
return bits