My solution use DP


  • 0
    D

    int* countBits(int num, int* returnSize) {
    int *ret = malloc(sizeof(int) * num+1);
    returnSize = num+1;
    ret[0] = 0;
    ret[1] = 1;
    ret[2] = 1;
    int i = 3;
    int n2 = 2;
    for(; i <= num; i++){
    if(i / (n2
    2) >= 1){
    if(i == n2 * 2){
    ret[i] = 1;
    continue;
    }else{
    n2 *= 2;
    }
    }
    ret[i] = ret[i - n2] + 1;;
    }
    return ret;
    }


Log in to reply
 

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