Simple solution


  • 0
    L
    class Solution {
    public:
        vector<int> countBits(int num) {
            vector<int> vec(num+1,-1);
            vec[0]=0;
            for(int i=1;i<=num;i++) vec[i]=(i&1)+vec[i>>1];
            return vec;
        }
    };

Log in to reply
 

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