Product of Array Except Self
class Solution {
public int[] productExceptSelf(int[] nums) {
if(nums == null && nums.length == 0){
return null;
}
int n = nums.length;
int[] ret = new int[n];
int[] left = new int[n];
int[] right = new int[n];
for(int i = 0; i < n; i ++){
if(i == 0){
left[i] = 1;
}else{
left[i] = left[i - 1] * nums[i - 1];
}
}
for(int i = n - 1; i >= 0; i --){
if(i == n - 1){
right[i] = 1;
}else{
right[i] = right[i + 1] * nums[i + 1];
}
}
for(int i = 0; i < n; i ++){
ret[i] = left[i] * right[i];
}
return ret;
}
}Last updated