My DP solution in C++


  • 0
    P

    I think it goes as faster as those bit tricks, but mine is more comprehensible.

    vector<int> countBits(int num) {
            int i = 1, j = 0;
            vector<int> answer(num + 1, 0);
            while(i <= num) {
                answer[i++] = answer[j++] + 1;
                if(!(i & (i-1))) {
                    j = 0;
                }
            }
            return answer;
        }
    

Log in to reply
 

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