Stupid but fast solution for c++


  • 1
    R

    somehow...it says my code beats 100% ppl in c++ (for c++ only).
    not the fastest solution among all the languages though.
    just for your reference.

    class Solution {
    public:
        vector<int> countBits(int num) {
            vector<int> array(1,0) ;
            int power=-1;
            int temp;
            while (num >= (2^power))
            {   power++;
                int size=array.size();
                for(int i=0;i<size;i++)
                {    if (array.size()>num)
                    {break;}
                    else
                    {array.push_back(array[i]+1);}
                }
            }
            return array;
        }
    };

Log in to reply
 

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