public class Solution {
public int[] multiply(int[] A) {
if (A == null || A.length == 0) {
return new int[0];
}
int length = A.length;
int[] B = new int[length];
B[0] = 1;
//计算下三角连乘
for (int i = 1; i< length; i++) {
B[i] = B[i-1] * A[i-1];
}
//计算上三角连乘
int temp = 1;
for (int j = length - 2; j >= 0; j--) {
temp *= A[j+1];
B[j] *= temp;
}
return B;
}
}
剑指offer1面试题52 构建乘积数组(java实现)
最新推荐文章于 2020-02-04 22:02:21 发布