A very easy to understand C solution


  • 0
    S
     * Return an array of size *returnSize.
     * Note: The returned array must be malloced, assume caller calls free().
     */
    int* productExceptSelf(int* nums, int numsSize, int *returnSize) {
    
        *returnSize = numsSize;
        int *firstarray = malloc(sizeof(int)*numsSize);
        int temp;
        int i = 0;
        
        int mul=1;
        for(i=0;i<numsSize;i++)
        {
            firstarray[i]=mul;
            mul=nums[i]*mul;
        }
        mul=1;
        for(i=numsSize-1;i>=0;i--)
        {
            temp=mul;
            firstarray[i]=firstarray[i]*temp;
            mul=nums[i]*mul;
        }
    
        return firstarray;
        
    }
    

Log in to reply
 

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