Steward
分享是一種喜悅、更是一種幸福
程式語言 - LeetCode - C - 338. Counting Bits
題目:

解答:
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* countBits(int n, int* returnSize)
{
int i = 0;
int j = 0;
int *r = calloc(n + 1, sizeof(int));
*returnSize = n + 1;
for (i = 0; i <= n; i++) {
int val = i;
for (j = 0; j < 64; j++) {
r[i] += (val & 1);
val >>= 1;
}
}
return r;
}