How efficient is this code for bit counting


  • 0
    T

    vector<int> countBits(int num) {

        vector<int> myvector;
         string str;
        int x=0;
        int counter=0;
        
        for(int i=0;i<=num;i++){
            int numb=i;
            while(numb!=0)
             {
                  x=numb%2;
                   if(x!=0){
                   counter++; }
        	   numb/=2;
               }
             myvector.push_back(counter);
             counter=0;
             x=0;
        }
       return myvector;
    }

Log in to reply
 

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