Java solution using DP


  • 0
    M
    public int[] countBits(int num) {
    	int[] counts = new int[num + 1];
    	counts[0] = 0;
    	int size = 1;
    	for (int i = 1; i <= num; i++) {
    		counts[i] = counts[i - size] + 1;
    		if ((i & (i + 1)) == 0) {
    			size = size << 1;
    		}
    	}
    	return counts;
    }

Log in to reply
 

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