程式語言 - LeetCode - C - 238. Product of Array Except Self



參考資訊:
https://www.cnblogs.com/grandyang/p/4650187.html

題目:


解答:

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* productExceptSelf(int* nums, int numsSize, int* returnSize)
{
    int i = 0;
    int f[100000] = { 0 };
    int b[100000] = { 0 };
    int *r = calloc(numsSize, sizeof(int));

    f[0] = 1;
    for (i = 0; i < (numsSize - 1); i++) {
        f[i + 1] = f[i] * nums[i];
    }
 
    b[numsSize - 1] = 1;
    for (i = numsSize - 1; i > 0; i--) {
        b[i - 1] = b[i] * nums[i];
    }

    *returnSize = numsSize;
    for (i = 0; i < numsSize; i++) {
        r[i] = f[i] * b[i];
    }

    return r;
}