Topic:
Given an array of integers a.
Define B[i] = a[0] * ... * a[i-1] * a[i+1] * ... * a[n-1], please do not use division when calculating B.
Sample Example
Given a=[1, 2, 3], return b for [6, 3, 2]
Solution: See sample, B[0]=a[1]*a[2],b[1]=a[0]*a[2],b[2]=a[0]*a[1];
Left in the code to save the value,
Right to save the value
classSolution { Public: /** @param nums:given an integers array A * @return: a long Long array B and b[i]= a[0] * ... * a[i-1] * a[i+1 ] * ... * a[n-1]*/Vector<Long Long> productexcludeitself (vector<int> &nums) { //Write your code here intn =nums.size (), I; Vector<Long Long> Left (n,1); Vector<Long Long> Right (N,1); Vector<Long Long> Res (n,1); for(i =1; I < n; ++i) left[i]= left[i-1] * nums[i-1]; for(i = n2; I >=0; --i) right[i]= right[i+1] * nums[i+1]; for(i =0; I! = Nums.size (); ++i) res[i]= Left[i] *Right[i]; returnRes; }};
Lintcode 50. The product of an array after rejecting an element