Python solution O(n)


  • 0
    L
    class Solution(object):
        def countBits(self, num):
            """
            :type num: int
            :rtype: List[int]
            """
            power = 0;
            result = [];
            result.append(0);
            #result.append(1);
            while len(result)<=num:
                for x in xrange(2**power):
                    result.append(result[x]+1);
                    if len(result)>num:
                        break;
                power += 1;
                        
            return result

  • 0
    A

    Can you tell me what your algorithm ideas, if you are willing to enlighten me, please contact me by email. thank you very mach.


Log in to reply
 

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