改进
for(int i =0; i<A.length ; i++ ){
int k = 0; leftarr[i] = 1; while(k<i){ leftarr[i] = leftarr[i] * A[k]; k++; } int j = A.length-1; rightarr[i] = 1; while(j>i){ rightarr[i] = rightarr[i] * A[j]; j--; } }
可以改成
left[0] = right[len - 1] = 1; for (int i = 1; i < len; i++) { left[i] = left[i - 1] * a[i - 1]; } for (int i = len - 2; i >= 0; i--) { right[i] = right[i + 1] * a[i + 1]; }
1.
import java.util.ArrayList;
public class Solution {
public int[] multiply(int[] A) {
int [] leftarr = new int [A.length];
int [] rightarr = new int [A.length];
for(int i =0; i<A.length ; i++ ){ int k = 0; leftarr[i] = 1; while(k<i){ leftarr[i] = leftarr[i] * A[k]; k++; } int j = A.length-1; rightarr[i] = 1; while(j>i){ rightarr[i] = rightarr[i] * A[j]; j--; } } int [] res = new int [A.length]; for(int i =0;i<A.length;i++){ if(i==0) res[i] = rightarr[i]; else if(i == A.length-1) res[i] = leftarr[i]; else res[i] = leftarr[i] * rightarr[i]; } return res; }
}