Easy to understand python solution (like a boss)


  • 0
    S

    Every time the number of digit increased by 1, we start from beginning and add 1.

        def countBits(self, num):
            """
            :type num: int
            :rtype: List[int]
            """
            d = 1
            result = [0]    
            j = 0
            for i in xrange(num):
                result.append(result[j]+1)
                if j+1 == d:
                    j = 0
                    d *= 2
                else:
                    j += 1
            return result```

Log in to reply
 

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