238. 除自身以外数组的乘积
238. Product of Array Except Self
题目描述
LeetCode238. Product of Array Except Self中等
Java 实现
class Solution {
public int[] productExceptSelf(int[] nums) {
int n = nums.length;
int[] before = new int[n], after = new int[n], res = new int[n];
before[0] = 1;
after[n - 1] = 1;
for (int i = 1; i < n; i++) {
before[i] = before[i - 1] * nums[i - 1];
}
for (int i = n - 2; i >= 0; i--) {
after[i] = after[i + 1] * nums[i + 1];
}
for (int i = 0; i < n; i++) {
res[i] = after[i] * before[i];
}
return res;
}
}
相似题目
参考资料