題目描述
給定一個(gè)數(shù)組A[0,1,...,n-1],請(qǐng)構(gòu)建一個(gè)數(shù)組B[0,1,...,n-1],其中B中的元素B[i]=A[0]A[1]...A[i-1]A[i+1]...A[n-1]稍途。不能使用除法镜沽。
public class Solution {
public int[] multiply(int[] A) {
if(A == null || A.length == 0){
return A;
}
int[] left = new int[A.length];
int[] right = new int[A.length];
int num = 1;
for(int i = 0; i < A.length; i++) {
left[i] = num;
num *= A[i];
}
num = 1;
for(int i = A.length - 1; i >=0; i--){
right[i] = num;
num *= A[i];
}
int[] B = new int[A.length];
for(int i = 0; i < A.length; i++) {
B[i] = left[i] * right[i];
}
return B;
}
}