Accepted C Solution nearly one pass with some explanation


  • 1
    R

    basic idea is scanning from both side of the array,and multiply the factors. no tricky at all.

    int* productExceptSelf(int* nums, int numsSize, int* returnSize) {
       if(0==numsSize) return 0; 
    
       int *ret=malloc(numsSize*sizeof(int));
       *returnSize=numsSize;
    
       int i=0;
       while(i<numsSize){
    	   ret[i]=1;
    	   ++i;
       }
    
       i=1;
       int right=1;
       int left=1;
       while(i<numsSize){
    	   left*=nums[i-1];
    	   ret[i]*=left;
    
    	   right*=nums[numsSize-i];
    	   ret[numsSize-i-1]*=right;
    	   ++i;
       }
    
       return ret;
    }

  • 0
    D

    I think this is a good solution!


Log in to reply
 

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