My C++ solution is 120ms. Is there any better one?


  • 0
    L
    vector<int> countBits(int num) {
        int sum = 0, mask = 1;
        vector<int> res(num + 1, 0);
        while (sum < num)
        {
            int count = min(num - sum, mask);
            for (int i = 0; i < count; i++)
                res[sum + i + 1] = res[i] + 1; 
            sum += count;
            mask <<= 1;
        }
        
        return res;
    }

Log in to reply
 

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