Leetcode 338 Solution

This article provides solution to leetcode question 338 (counting-bits)

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

Solution

class Solution {
public:
    vector<int> countBits(int num) {
        vector<int> res(num + 1);
        res[0] = 0;

        for (int i = 1; i <= num; i++)
            res[i] = res[i & (i - 1)] + 1;

        return res;
    }
};