题目链接:
力扣[这里是图片001]https://leetcode-cn.com/problems/product-of-array-except-self/
【分析】算出当前元素的前缀乘 和 后缀乘即可。
class Solution {
public int[] productExceptSelf(int[] nums) {
int n = nums.length;
int i;
int[] l = new int[n + 1]; l[0] = 1;
int[] r = new int[n + 1]; r[n] = 1;
for(i = 0; i < n; i++){
l[i + 1] = l[i] * nums[i];
}
for(i = n - 1; i >= 0; i--){
r[i] = r[i + 1] * nums[i];
}
int[] ans = new int[n];
for(i = 0; i < n; i++){
ans[i] = l[i] * r[i + 1];
}
return ans;
}
}