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;
    }
}

相似题目

参考资料

相关文章:

  • 2021-10-19
  • 2022-02-19
  • 2021-08-20
  • 2022-02-14
  • 2021-11-28
  • 2021-11-20
猜你喜欢
  • 2021-11-04
  • 2022-12-23
  • 2022-12-23
  • 2021-09-24
  • 2022-12-23
  • 2021-08-13
相关资源
相似解决方案