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> v;
int n = 0;
for(int i=0; i<=num; i++)
{
n = 0;
int m = i;
while(m > 0)
{
if(m & 1 == 1)
n++;
m >>= 1;
}
v.push_back(n);
}
return v;
}
};