key: make use of what’s produced before
Runtime: 88 ms / beats 53.16%
Reference: blog, discuss
class Solution {
public:
vector<int> countBits(int num) {
vector<int> res(num+1, 0);
for(int i=0; i<=num; i++ )
res[i] = res[i>>1] + (i&1);
return res;
}
};