6-line python solution, simple and efficient


  • 0
    Z
        def countBits(self, num):
        """
        :type num: int
        :rtype: List[int]
        """
        
        relist = [0]
        power = 2
        for k in range(1,num+1):
            if k >= power: 
                power *= 2
            relist.append(relist[k-power/2]+1)
            
        return relist

Log in to reply
 

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