C++ 132ms, 4 line solution


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

Log in to reply
 

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