338. Counting Bits (Easy) - TengnanYao/daily_leetcode GitHub Wiki

class Solution:
    def countBits(self, n: int) -> List[int]:
        result = [0]
        i = 1
        while i <= n:
            result += list(map(lambda x : x + 1, result))
            i <<= 1
        return result[ : n + 1]