leetcode-338. counting bits

https://leetcode.com/problems/counting-bits/
Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1’s in their binary representation and return them as an array.

1
2
3
4
5
6
7
8
public int[] countBits(int num) {
int[] count = new int[num + 1];
count[0] = 0;
for(int i = 1; i <= num; i++) {
count[i] = count[i/2] + i % 2;
}
return count;
}