Share my c++ solution


  • 0
    Z
    vector<int> countBits(int num) {
        vector<int> res;
        res.push_back(0);
        if(num==0)
            return res;
        res.push_back(1);
        if(num==1)
            return res;
        int chushu=1;
        for(int i=2;i<=num;i++)
        {
            if(i==chushu*2)
            {
                res.push_back(1);
                chushu*=2;
            }
            else
            {
                res.push_back(res[i%chushu]+1);
            }
        }
        return res;
    }

Log in to reply
 

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