My c solution 40s


  • 4
    A

    int* countBits(int num, int* returnSize) {
    if (num < 0 ) {
    return NULL;
    }

    int *dp = (int *) malloc(sizeof(int)* (num + 1));
    *returnSize = num + 1;
    dp[0] = 0;
    
    for (int i =1; i <= num; i++) {
        if (i & 1) {
            dp[i] = 1 + dp[i - 1];
        } else {
            dp [i] = dp[i >> 1];
        }
    }
    
    return dp;
    

    }


Log in to reply
 

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