Accepted Java solution


  • 0
    O
    public class Solution {
        public int[] countBits(int num) {
        	int[] result = new int[num+1];
            if (num == 0) {
            	result[0] = 0;
            	return result;
            }
            result[0] = 0;
            for(int i = 1; i < num+1; i++) {
            	result[i] = 1;
            }
            int pos = 2, i = 2;
            while(i < num+1) {
            	for(int j = 0; j < pos && i < num+1; j++) {
            		result[i++] += result[j]; 
            	}
            	pos *= 2;
            }
            return result;
        }
    }

Log in to reply
 

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