DP Java solution 3ms


  • 0
    T
    public int[] countBits(int num) {
            int[] bits = new int[num+1];
            if(num == 0)
                return bits;
    
            bits[1] = 1;
            int counter = 2;
            int bitcounter;
    
            for(int i=2;i<=num || counter <= num;) {
                bitcounter = 0;
                bits[counter++] = 1 ;
                int next = i*2;
                while(counter <next && counter <= num)
                    bits[counter++] = 1 + bits[++bitcounter];
    
                i = next;
            }
    
            return bits;
        }

Log in to reply
 

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