/* All Contributors (C) 2020 */
package io.github.dreamylost.practice;

/**
 * @description 给定一个数组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]。不能使用除法。
 *     <p>//B[i]=A[0]*A[1]*...*A[i-1]*A[i+1]*...*A[n-1] //从左到右算 B[i]=A[0]*A[1]*...*A[i-1]
 *     //从右到左算B[i]*=A[i+1]*...*A[n-1]
 * @author Mr.Li
 */
public class Multiply {

    public int[] multiply(int[] A) {
        int length = A.length;
        int[] B = new int[length];
        if (length != 0) {
            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;
    }
}
