class Solution:
# 遍历
def countBits(self, num):
"""
:type num: int
:rtype: List[int]
"""
res = []
for i in range(num+1):
res.append(bin(i)[2:].count('1'))
return res
class Solution:
# 动态规划
def countBits(self, num):
"""
:type num: int
:rtype: List[int]
"""
dp = [0]
for i in range(1, num + 1):
dp.append(dp[i & (i-1)] + 1)
return dp
---------------------作者:zzc15806 来源:CSDN 原文:https://blog.csdn.net/zzc15806/a ... 062?utm_source=copy 版权声明:本文为博主原创文章,转载请附上博文链接!
|
|