LeetCode 238. 除自身以外数组的乘积(Product of Array Except Self)

2023-05-13,,

238. 除自身以外数组乘积

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/

LeetCode 238. 除自身以外数组的乘积(Product of Array Except Self)的相关教程结束。

《LeetCode 238. 除自身以外数组的乘积(Product of Array Except Self).doc》

下载本文的Word格式文档,以方便收藏与打印。