//https://leetcode.com/problems/counting-bits/
int bin(int n)
{
int c=0;
while(n>0){
if(n%2==1)
c++;
n=n/2;
}
return c;
}
int countBits(int n, int* returnSize){
int ans=(int)calloc((n+1),sizeof(int));
for(int i=0;i<=n;i++)
ans[i]=bin(i);
return ans;
}
Hey @negia
In Function int countBits(int n, int* returnSize) you are not returning pointer array you have to declare int*, also returnSize also needs to be updated,Also Malloc must be used as it is said in the editor.
Here is your code I have modified.
int bin(int n)
{
int c=0;
while(n>0){
if(n%2==1)
c++;
n=n/2;
}
return c;
}
int* countBits(int n, int* returnSize){
int *ans;
ans = malloc((n+1)*sizeof(int));
for(int i=0;i<=n;i++)
ans[i]=bin(i);
*returnSize = n+1;
return ans;
}