Simple C++ Solution using mod operator o(n)


  • 0
    P

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


Log in to reply
 

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