Java Solution


  • 0
    Y
    public class Solution {
        public int[] countBits(int num) {
            int[] count = new int[num + 1];
            count[0] = 0;
            if(num >= 1) count[1] = 1;
            if(num >= 2) count[2] = 1;
            if(num >= 3) count[3] = 2;
            int index = 0;
            int range = 3;
            for(int i=4; i<=num; i++){
                if(index == range + 1){
                    index = 0;
                    range = i - 1;
                }
                if(index == 0)
                    count[i] = 1;
                else
                    count[i] = 1 + count[i - 1 - range];
                index += 1;
            }
            
            return count;
        }
    }

Log in to reply
 

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