Single pass Python Solution


  • 0
    S
    class Solution(object):
        def countBits(self, num):
            """
            :type num: int
            :rtype: List[int]
            """
            if num == 0:
                return [0]
            a=[0,1]
            power = 1
            for i in xrange(2,num+1):
                if i%(power)==0:
                    power=i
                    a.append(1)
                else:
                    a.append(a[i-power]+1)
            
            return a

Log in to reply
 

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