238. Product of Array Except Self
Medium
Given an array
nums of n integers where n > 1, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].
Example:
Input:[1,2,3,4]Output:[24,12,8,6]
Note: Please solve it without division and in O(n).
Follow up:
Could you solve it with constant space complexity? (The output array does not count as extra space for the purpose of space complexity analysis.)
Could you solve it with constant space complexity? (The output array does not count as extra space for the purpose of space complexity analysis.)
Solution:
1) Construct such a temporary array left[i] which contains the product of all the elements on left side of element i exclude element i.
2) Construct such a temporary array right[i] which contains the product of all the elements on right side of element i exclude element i.
3) get the prod[], which multiply left[] and right[]
public int[] productExceptSelf(int[] nums) {
if(nums == null) return null;
int n = nums.length;
int[] left = new int[n];
int[] right = new int[n];
left[0] = 1;
for(int i = 1 ; i < n; i++){
left[i] = nums[i-1] * left[i-1];
}
right[n-1] = 1;
for(int i = n - 2; i>=0; i--){
right[i] = nums[i+1] * right[i+1];
}
int[] res = new int[n];
for(int i = 0; i < n ; i++){
res[i] = left[i] * right[i];
}
return res;
}
Comments
Post a Comment