Simple DP java solution O(n)


  • 0
    C
    public int[] countBits(int num) {
            int[] dp = new int[num+1];
            for(int i=1;i<=num;i++){
                int temp = i;
                while(temp % 2 == 0) temp /= 2;
                temp /=2;
                dp[i] = dp[temp]+1;
            }
            return dp;
        }
    

Log in to reply
 

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