[338][中等][动态规划] 比特位计数
最后更新于
最后更新于
class Solution:
def countBits(self, num: int) -> List[int]:
bits = [0]
for i in range(1, num + 1):
if i & 1 == 0:
bits.append(bits[i // 2])
else:
bits.append(bits[i - 1] + 1)
return bitsclass Solution:
def countBits(self, num: int) -> List[int]:
bits = [0]
highBit = 0
for i in range(1, num + 1):
if i & (i - 1) == 0:
highBit = i
bits.append(bits[i - highBit] + 1)
return bitsclass Solution:
def countBits(self, num: int) -> List[int]:
bits = [0]
for i in range(1, num + 1):
bits.append(bits[i & (i - 1)] + 1)
return bits