class Solution {
//给定一个数组 A[0,1,…,n-1],请构建一个数组 B[0,1,…,n-1],其中 B[i] 的值是数组 A 中除了下标 i 以外的元素的积, 即 B[i]=A[0]×A[1]×…×A[i-1]×A[i+1]×…×A[n-1]。不能使用除法。
//先下三角计算0-i-1,再上三角计算i+1-n
public int[] constructArr(int[] a) {
if(a==null||a.length==0){
return new int[0];
}
int []B=new int[a.length];
int temp=1;
B[0]=1;
for(int i=1;i<a.length;i++){
B[i]=B[i-1]*a[i-1];
}
for(int i=a.length-2;i>=0;i--){
temp=temp*a[i+1];
B[i]=B[i]*temp;
}
return B;
}
}
浙公网安备 33010602011771号