The idea is simple. If num is even, then number of 1s will be equal to nums [num/2] ; else, number of 1s equal to nums [num - 1] + 1;

```
public int[] countBits(int num) {
int[] nums = new int[num+1];
nums[0] = 0;
for (int i = 1; i < num + 1; i ++) {
if ((i & 1) == 0) {
nums[i] = nums[i >> 1];
}
else {
nums[i] = nums[i-1] + 1;
}
}
return nums;
}
```