3 line C++ solution


  • 1
    class Solution {
    public:
        vector<int> countBits(int num) {
            vector<int> res = {0};
            for(int i=1; i<num+1; i++) res.push_back(res[i/2] + i%2);
            return res;
        }
    };
    

Log in to reply
 

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