Skip to content

Commit

Permalink
fix: clean up 0338-counting-bits.c
Browse files Browse the repository at this point in the history
- Use calloc() instead of malloc() and memset().
- Use bit shifting instead of conditionally checking an offset.
  • Loading branch information
andrewmustea committed Jan 29, 2023
1 parent 1349c24 commit da4af2a
Showing 1 changed file with 20 additions and 17 deletions.
37 changes: 20 additions & 17 deletions c/0338-counting-bits.c
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
/**
* Given an integer n, return an array ans of length n + 1 such that for each i
* (0 <= i <= n), ans[i] is the number of 1's in the binary representation of i.
*
* Constraints:
*
* 0 <= n <= 10^5
*
* Note: The returned array must be malloced, assume caller calls free().
*
* Space: O(1)
* Time: O(n)
*/
int* countBits(int n, int* returnSize){
// Initialize array of size n + 1
int arraySize = n + 1;
*returnSize = arraySize;
int *dp = (int *)malloc(sizeof(int)*arraySize);
memset(dp, 0, arraySize*sizeof(dp[0]));
int offset = 1;

// Perform dp
for (int i = 1; i <= n; i++) {
if (offset * 2 == i) {
offset = i;
}
dp[i] = 1 + dp[i - offset];
}
return dp;
}

int *countBits(int n, int *returnSize) {
int *ret = calloc(n + 1, sizeof(int));
*returnSize = n + 1;

for (int i = 1; i <= n; ++i)
ret[i] = ret[i >> 1] + (i & 1);

return ret;
}

0 comments on commit da4af2a

Please sign in to comment.