Simple, Easy-to-understand Python Solution


  • 0
    F
    def countBits(self, num):
        """
        :type num: int
        :rtype: List[int]
        """
        pow2 = 1
        cache = [0]
        for n in range(1, num+1):
            if n >= pow2 * 2:
                pow2 = pow2 * 2
            cache.append(cache[n-pow2] + 1)
        return cache

Log in to reply
 

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