5ms solution- java


  • 4
    N

    public int[] countBits(int num) {

        int dp[] = new int[num + 1];
        for (int i = 1 ; i <= num ; i++) {
            if (i % 2 == 0) {
                dp[i] = dp[i/2];//even
            } else {
                dp[i] = dp[i-1] + 1;//odd
            }
        }
        return dp;
    

    }


Log in to reply
 

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