238. Product of Array Except Self
题目描述
LeetCode
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;
}
}
相似题目
42. 接雨水 Trapping Rain Water
152. 乘积最大子序列 Maximum Product Subarray
265. 粉刷房子 II Paint House II
参考资料
https://leetcode.com/problems/product-of-array-except-self/
https://leetcode-cn.com/problems/product-of-array-except-self/