simple solution 29ms in C with O(n)


  • 0
    W
    
    	int *p;
    	p = (int*)malloc(numsSize*sizeof(int));
    	int sum = 1;
    	int zero = 0;
    	for (int i = 0; i<numsSize; i++)
    	{
    		if (nums[i] == 0)
    			zero++;
    		else
    			sum = sum*nums[i];
    	}
    
    	if (zero == 0)
    	{
    		for (int i = 0; i<numsSize; i++)
    			p[i] = sum / nums[i];
    }
    	else if (zero == 1)
    	{
    		for (int i = 0; i<numsSize; i++)
    			if (nums[i] == 0)
    				p[i] = sum;
    			else
    				p[i] = 0;
    	}
    	else
    	{
    		for (int i = 0; i<numsSize; i++)
    			p[i] = 0;
    	}
    	*returnSize = numsSize;
    	return p;
    }
    

Log in to reply
 

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