Python Solution Easy to Understand, O(n) time


  • 0
    B
    class Solution(object):
        def countBits(self, num):
            """
            :type num: int
            :rtype: List[int]
            """
            results = [0] * (num+1)
            if num > 0:
                for i in range(1, num+1):
                    if i % 2 == 0:
                        results[i] = results[i/2]
                    else:
                        results[i] = results[i-1] + 1
            return results
    

Log in to reply
 

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