编程题day10

链接:https://www.nowcoder.com/questionTerminal/94a4d381a68b47b7a8bed86f2975db46
来源:牛客网
给定一个数组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]。不能使用除法。

import java.util.ArrayList;
public class Solution {
    public int[] multiply(int[] A) {
        int[] b=new int[A.length];
        //第一个for循环完成上三角乘法
        b[0]=1;
        for(int i=1;i<b.length;i++){
            b[i]=b[i-1]*A[i-1];
        }
        //第二个for循环完成下三角惩罚
        int temp=1;
        for(int j=b.length-2;j>=0;j--){
            temp=temp*A[j+1];
            b[j]=b[j]*temp;
        }
    return b;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_40163148/article/details/83276475