Python O(n) time 1 pass O(n) space


  • 0
    D

    import math
    class Solution(object):
    def countBits(self, num):
    """
    :type num: int
    :rtype: List[int]
    """
    if num == 0:
    return [0]

        m = math.ceil(math.log(num) / math.log(2))
        
        i = 0
        result = [0]
        cur = [1]
        result += cur
        while(i <= m):
            cur += [j + 1 for j in cur]
            result += cur
            i += 1
        
        return result[0:num + 1]

Log in to reply
 

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