My Normal C Solution


  • 0
    S
    int* countBits(int num, int* returnSize) {
        *returnSize = num+1;
        int *r = malloc(sizeof(int)*(*returnSize));
        r[0] = 0;
        int i=1,n=1;
        for(; i<=num;i++ ){
            if (i%2 == 0){
                if(i == 2<<n){
                    r[i] = 1;
                    n++;
                }
                else{
                    r[i] = r[i-(2<<(n-1))]+1;
                }
            }
            else{
                r[i] = r[i-1] + 1;
            }
        }
        return r;
    }
    

Log in to reply
 

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