C++ solution, O(n) time, one pass


  • 0
    1
    vector<int> countBits(int num) {
        vector<int> result;
        result.push_back(0);
        int itr=0;
        while(itr<num){
            int size=result.size();
            for(int i=0;i<size;++i){
                if(itr<num){
                    ++itr;
                    result.push_back(result[i]+1);
                }
                else
                    return result;
            }
        }
        return result;
    }

Log in to reply
 

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