```
public class Solution {
public int[] countBits(int num) {
int[] res = new int[num + 1];
boolean even = true;
for (int i = 0; i <= num; i++) {
if (even) {
res[i] = res[i / 2];
even = false;
}
else {
even = true;
res[i] = res[i / 2] + 1;
}
}
return res;
}
}
```