Use log and pow function to solve this question C++


  • 0
    J
    vector<int> countBits(int num) {
        vector<int> res;
        
        for(int i=0;i<=num;++i){
            if(i<2){
                res.push_back(i);
            }else{
                int counts = 0;
               int newnum = log2(i);
               counts=(newnum==0?0:1);
               counts +=res[i-(pow(2,newnum))];
               res.push_back(counts);
            }
           
        }
        return res;
    }

Log in to reply
 

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