Python solution one pass


  • 0
    P
    class Solution(object):
        def countBits(self, num):
            """
            :type num: int
            :rtype: List[int]
            """
            dp = [0] * (num + 1)
            # last power of 2
            last = 1
            for i in range(1, len(dp)):
                if i & (i - 1) == 0:
                    last = i
                    dp[i] = 1
                else:
                    dp[i] = 1 + dp[i - last]
            return dp
    

Log in to reply
 

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