class Solution {
public int[] countBits(int num) {
int[] res = new int[num + 1];
res[0] = 0;
int pow = 1;
for (int i = 1, t = 0; i <= num; i++, t++) {
if (i == pow) {
pow *= 2;
t = 0;
}
res[i] = res[t] + 1;
}
return res;
}
}
With this, we can only find the math pattern behind it.
With this, we can only find the math pattern behind it.