Dynamic programming C++ solution with some bit manipulations.


  • 1
    S
        vector<int> countBits(int num) {
            vector<int> result;
            result.push_back(0);
            for (int i=1;i<=num;i++){
                result.push_back(result[i>>1] + (i&1));
            }
            return result;
        }
    

Log in to reply
 

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