Share my C code, no extra space or division


  • 0
    M
    int* productExceptSelf(int* nums, int numsSize, int* returnSize) {
    int *output = (int *)malloc(sizeof(int)*numsSize);
    *returnSize = numsSize;
    
    *output = 1;
    for (int i=1; i<numsSize; i++)
        output[i] = output[i-1] * nums[i-1];
    
    int temp = nums[numsSize-1];
    for (int i=numsSize-2; i>=0; i--) {
        output[i] *= temp;
        temp *= nums[i];
    }
    
    return output;
    

    }


Log in to reply
 

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