A simple solution in python


  • 0
    X
    `
    class Solution(object):
        def countBits(self, num):
            """
            :type num: int
            :rtype: List[int]
            """
            min_num, max_num = 0, 1 
            arry = []
            arry.append(0) # 0 is the first element
            for i in range(1,num+1):
                if i < max_num:
                    arry.append(arry[i-min_num]+1)
                else:
                    arry.append(1)
                    min_num = max_num
                    max_num = max_num << 1
            return arry
    `

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.