```
public class Solution {
public int[] countBits(int num) {
int[] result = new int[num + 1];
result[0] = 0;
int refNum = 0;
for (int i = 1; i <= num; i++) {
if ((i & (i - 1)) == 0) {
result[i] = 1;
refNum = i;
}
else {
int temp = i - refNum;
result[i] = 1 + result[temp];
}
}
return result;
}
}
```