Memoization based Python Solution O(N) in space and time


  • 0
    U
    class Solution(object):
        def countBits(self, num):
            """
            :type num: int
            :rtype: List[int]
            """
            ans = [0] * (num + 1)
            for number in xrange(1, num + 1):
                ans[number] = ans[number >> 1]
                if number % 2 == 1:
                    ans[number] += 1
            
            return ans
    

Log in to reply
 

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