Topic: Product of arrays except itself
Given an integer array nums , return the array answer , where answer[i] is equal to the product of the remaining elements in nums except nums[i] .
The question data ensures that the product of all prefix elements and suffixes of any element in the array nums is within the range of 32-bit integers.
Please do not use division and complete this problem within O(n) time complexity.
hint:
2 <= nums.lengh <= 10^5
-30 <= nums[i] <= 30
Ensure that the product of all prefix elements and suffixes of any element in the array is within the 32-bit integer range nums;
Example 1:
Input: nums=[1, 2, 3, 4]
Output: [24, 12, 8, 6]
Example 2:
Input: nums=[-1,-1,0,-3,3]
Output: [0, 0, 9, 0, 0]
Problem-solving ideas:
Define two arrays (prefix product array and suffix product array) , the prefix array is: left [] , the suffix array: right [];
Left [ i ] stores the sum of products before nums [ i ] , and righ [ i ] stores the sum of products after nums [ i ] (stores 1 when empty);
Create a dynamic memory variable int* ret=(int*)malloc(sizeof(int)*numsSize);
Then ret [ i ] = left [ i ] * right [ i ], return after storage;
Idea implementation:
Because array elements : 2 <= nums.lengh <= 10^5
Therefore, the array sizes requested for prefix and suffix arrays are both 10^5;
int left[100000]={0};
int right[100000]={0};
Then it is to traverse the values of the prefix product array:
//前缀乘积和
for(i=0;i<numsSize;i++)
{
if(i==0)
{
left[i]=1;
}
else
{
left[i]=left[i-1]*nums[i-1];
}
}
Analysis:
Here we use the array nums [1, 2, 3, 4, 5, 6] as an example;
At this time, left [ 0 ] = 1, then left [ 1 ] = left [ 0 ] * nums [ 0 ] is equivalent to left [ 1 ] = nums [ 0 ];
left [ 2 ] = left [ 1 ] * nums [ 1 ] 相当于 left [ 2 ] = nums [ 0 ] * nums [ 1 ] ;
left [ 3 ] = left [ 2 ] * nums [ 2 ] 相当于 left [ 3 ] = nums [ 0 ] * nums [ 1 ] * nums [ 2 ] ;
。。。。。。
left [ 5 ] = left [ 4 ] * nums [ 4 ] 相当于 left [ 5 ] = nums [ 0 ] * nums [ 1 ] * nums [ 2 ] * nums [ 3 ] * nums [ 4 ] ;
I believe everyone has found the pattern;
Then it is to traverse the values of the suffix product array:
//后缀乘积和
for(i=numsSize-1;i>=0;i--)
{
if(i==numsSize-1)
{
right[i]=1;
}
else
{
right[i]=right[i+1]*nums[i+1];
}
}
Analysis:
Similarly: nums [1, 2, 3, 4, 5, 6]
left [ 5 ] =1, then left [ 4 ] = left [ 5 ] * nums [ 5 ] is equivalent to left [ 4 ] = nums [ 5 ];
left [ 3 ] = left [ 4 ] * nums [ 4 ] 相当于 left [ 3 ] = nums [ 4 ] * nums [ 5 ] ;
left [ 2 ] = left [ 3 ] * nums [ 3 ] 相当于 left [ 2 ] = nums [ 3 ] * nums [ 4 ] * nums [ 5 ] ;
。。。。。。
left [ 0 ] = left [ 1 ] * nums [ 1 ] 相当于 left [ 0 ] = nums [ 1 ] * nums [ 2 ] * nums [ 3 ] * nums [ 4 ] * nums [ 5 ] ;
Then it is to assign a value to the return value (* returnSize), create a dynamic inner (ret), assign a value to it and then return;
* returnSize=numsSize;
int* ret=(int*)malloc(4*numsSize);
//给返回指针赋值
for(i=0;i<numsSize;i++)
{
ret[i]=left[i]*right[i];
}
The time complexity is (O(N));
This is the basic idea of this question. The following is the program source code:
int* productExceptSelf(int* nums, int numsSize, int* returnSize){
int i=0;
int left[100000]={0};
int right[100000]={0};
//前缀乘积和
for(i=0;i<numsSize;i++)
{
if(i==0)
{
left[i]=1;
}
else
{
left[i]=left[i-1]*nums[i-1];
}
}
//后缀乘积和
for(i=numsSize-1;i>=0;i--)
{
if(i==numsSize-1)
{
right[i]=1;
}
else
{
right[i]=right[i+1]*nums[i+1];
}
}
* returnSize=numsSize;
int* ret=(int*)malloc(4*numsSize);
//给返回指针赋值
for(i=0;i<numsSize;i++)
{
ret[i]=left[i]*right[i];
}
return ret;
}
If there are any deficiencies, please feel free to supplement and communicate!
end. . .