Counting Bits

 https://leetcode.com/problems/counting-bits/

class Solution {
public:
    vector<int> countBits(int num) {
         vector<int> ans;
        for(int i=0;i<=num;i++){
            int curr=i;
            int count=0;
            while(curr){
                if(curr&1){
                    count++;
                }
                curr=curr>>1;
            }
            ans.push_back(count);
        }
        return ans;
    }
};

Comments

Popular posts from this blog

Perfect Peak of Array

Is Rectangle?

Sort array with squares!