给定一个整数数组 nums
,返回 数组 answer
,其中 answer[i]
等于 nums
中除 nums[i]
之外其余各元素的乘积 。(不使用除法)
class Solution {
public int[] productExceptSelf(int[] nums) {
int len = nums.length;
int [] L = new int [len];
int [] R = new int [len];
int [] anwser = new int [len];
//左侧乘积
L[0]=1;
for(int i=1;i<len;i++){
L[i] = L[i-1]*nums[i-1];
}
//右侧乘积
R[len-1]=1;
for(int i=len-2;i>=0;i--){
R[i]=R[i+1]*nums[i+1];
}
for(int i=0;i<len;i++){
anwser[i]=L[i]*R[i];
}
return anwser;
}
}