Share my 4ms AC JAVA solution


  • 0
    W
    public class Solution {
    int[] ans;
    public int[] countBits(int num) {
        ans = new int[num+1];
        if (num == 0)
            return ans;
        countBitsHelper(num, 1, 2);
        return ans;
    }
    
    private void countBitsHelper(int num, int start, int end) {
        int i;
        for (i=start; i<end && i<=num; i++) {
            ans[i] = 1 + ans[i-start];
        }
        if (end <= num)
            countBitsHelper(num, i, 2*i);
        else
            return;
    }
    

    }


Log in to reply
 

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