看注释
import java.util.ArrayList; public class Solution { public int[] multiply(int[] A) { //使用一个临时变量temp记录A[i]的原始值 //然后将A[i]=1 //计算B[i]=A[0]A[1]...A[i-1] A[i] A[i+1]...A[n-1],因为A[i]=1,所以无需剔除 //最后将A[i]还原 //算法很巧妙 int[] B = new int[A.length]; int temp = 0; for (int i = 0; i < A.length; i++) { temp = A[i]; A[i] = 1; B[i] = 1; for (int j = 0; j < A.length; j++) { B[i] *= A[j]; } A[i] = temp; } return B; } }