Approach
Need to couting bits for each n, and this is actually the number of bits for n/2 and plusing n%2.
Code
class Solution {
public:
vector<int> countBits(int n) {
if(n == 0) return {0};
if(n == 1) return {0, 1};
vector<int> ans(n+1);
ans[0] = 0;
ans[1] = 1;
for(int i = 2 ; i <= n ; ++i) {
ans[i] = ans[i/2] + (i % 2);
}
return ans;
}
};