C# solution with nested loop


  • 0
    M
    public int[] CountBits(int num) {
        int[] r = new int[num + 1];
        int i = 1;
        
        while(i < num + 1) {
            int c = Math.Min(i, num - i + 1);
            for(int j = 0; j < c; j++)
                r[i++] = r[j] + 1;
        }
        
        return r;
    }

Log in to reply
 

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