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.
Example:
For num = 5
you should return [0,1,1,2,1,2]
.
class Solution {
public:
vector<int> countBits(int num) {
vector<int> bits(num+1, 0);
for(int j = 0; j <= num; j ++)
{
for(int i = 31; i >= 0; i --)
{
if(((j >> i) & 1) == 1)
bits[j] ++;
}
}
return bits;
}
};