【题目描述】
Acwing 86. 构建乘积数组
【思路】
不能使用除法 那么就分两个部分计算,B[i] ,是i之前的数累乘 和 i之后的数累乘 的乘积。
class Solution {
public int[] multiply(int[] A) {
if( A== null || A.length == 0) return A;
int n = A.length;
int [] B = new int[n];
B[0] = 1;
for(int i = 1, p = A[0]; i < n; i ++){
B[i] = p;
p = p * A[i];
}
for(int j = n - 2, p = A[ n - 1]; j >= 0; j --){
B[j] *= p;
p = p * A[ j];
}
return B;
}
}