Easy to understand C++ solution


  • 6
    X
    vector<int> countBits(int num) {
       //if the ith number is even, the bit count is the same as i/2; otherwise the bit count is one more than i/2.
        vector<int> res(num+1,0);
        for(int i=1;i<=num;i++){
            if (i%2==1) res[i]= res[i/2]+1;
            else res[i]= res[i/2];
        }
        return res;
    }

Log in to reply
 

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