C Solution 23ms


  • 0
    Z

    int* countBits(int num, int* returnSize) {
    *returnSize = num +1 ;
    int *array = (int *)malloc(sizeof(int) * (num +1));
    array[0] =0;
    int i,count = 1;
    for(i=1; i<=num; i++){
    if(count == 1){
    array[i] = array[i/2] + 1;
    count--;
    }else{
    array[i] = array[i/2];
    count++;
    }

    }
    
    return array;
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.