```
public class Solution {
public int[] countBits(int num) {
// DP
int[] result = new int[num+1];
int offset = 1;
for (int i = 1; i <= num; i ++){
if (i >= offset * 2){
offset *= 2;
}
result[i] = result[i - offset] + 1;
}
return result;
}
// Time: O(n), Space: O(1) exclude output array
}
```