剑指offer-构建乘积数组

题目描述:给定一个数组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]。不能使用除法。

ac代码:

 1 import java.util.ArrayList;
 2 public class Solution {
 3     public int[] multiply(int[] A) {
 4              int[]s=new int[A.length];
 5         for(int i=0;i<s.length;i++){
 6             int sum=1;
 7             for(int j=0;j<s.length;j++){
 8                 if(j!=i){
 9                     sum=sum*A[j];
10                 }
11             }
12             s[i]=sum;
13         }
14         return s;
15     }
16 }
原文地址:https://www.cnblogs.com/llsq/p/8809866.html