My solution in Java


  • 0
    C
    public class Solution{
        public int[] countBits(int num){
            int[] arr=new int[num+1];
            arr[0]=0;
            for (int i=1;i<num+1;i++){
               arr[i]=((i%2)==0)?arr[i/2]:arr[i-1]+1;
            }
            return arr;
        }
    }

Log in to reply
 

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