Naive implementation


  • 1
    N
    public class Solution {
        public int[] countBits(int num) {
            int[] counts=new int[num+1];
            if(num==0) return counts;
            counts[1]=1; int prev=1; int next=2;
            for(int i=1;i<=num;i++){
               if(i<next){
                   counts[i]=counts[prev]+counts[i-prev];
               }
               if(i==next){
                   counts[i]=1;
                   prev=next;
                   next=prev*2;
               }
            }
            return counts;
        }
    }

Log in to reply
 

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