338. 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.

Example:
For num = 5 you should return [0,1,1,2,1,2].

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class  {
public:
vector<int> countBits(int num) {
int temp,count;
vector<int>result;
for(int i=0;i<=num;i++){
temp=i;
count=0;
while(temp){
count+=temp&1;
temp>>=1;
}
result.push_back(count);


}
return result;

}
};