给定一个数组A[0,1,…,n-1],请构建一个数组B[0,1,…,n-1],其中B中的元素B[i]=A[0]A[1]…*A[i-1]A[i+1]…*A[n-1]。不能使用除法。(注意:规定B[0] = A[1] * A[2] * … * A[n-1],B[n-1] = A[0] * A[1] * … * A[n-2];)
对于A长度为1的情况,B无意义,故而无法构建,因此该情况不会存在。
import java.util.ArrayList;
public class Solution {
public int[] multiply(int[] A) {
int [] res = new int [A.length];
if(A.length <= 1) {
return new int [0];
}
res[0] = 1;
//从前向后乘
for(int i= 1; i< A.length; i++){
res[i] = A[i-1] * res[i-1];
}
// 从后向前乘
for (int i = A.length -1, p = 1; i >=0; i--) {
res[i] *= p;
p *= A[i];
}
return res;
}
}