友情支持

如果您觉得这个笔记对您有所帮助,看在D瓜哥码这么多字的辛苦上,请友情支持一下,D瓜哥感激不尽,😜

支付宝

微信

有些打赏的朋友希望可以加个好友,欢迎关注D 瓜哥的微信公众号,这样就可以通过公众号的回复直接给我发信息。

wx jikerizhi

公众号的微信号是: jikerizhi因为众所周知的原因,有时图片加载不出来。 如果图片加载不出来可以直接通过搜索微信号来查找我的公众号。

238. 除自身以外数组的乘积

给你一个整数数组 nums,返回 数组 answer ,其中 answer[i] 等于 nums 中除 nums[i] 之外其余各元素的乘积 。

题目数据 保证 数组 `nums`之中任意元素的全部前缀元素和后缀的乘积都在 32 位 整数范围内。

不要使用除法,且在 \(O(n)\) 时间复杂度内完成此题。

示例 1:

输入: nums = [1,2,3,4]
输出: [24,12,8,6]

示例 2:

输入: nums = [-1,1,0,-3,3]
输出: [0,0,9,0,0]

提示:

  • 2 <= nums.length <= 105

  • -30 <= nums[i] <= 30

  • 保证 数组 nums 之中任意元素的全部前缀元素和后缀的乘积都在 32 位 整数范围内

进阶:你可以在 O(1) 的额外空间复杂度内完成这个题目吗?(出于对空间复杂度分析的目的,输出数组 不被视为 额外空间。)

思路分析

利用 Prefix Sum 前缀和 的思路,分别求前缀乘积和后缀乘积,然后再逐个求每个元素的前后元素乘积。

利用结果数组存前缀乘积,可以将空间复杂度降低到常数级。

  • 一刷

  • 二刷

  • 三刷

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
 * Runtime: 1 ms, faster than 100.00% of Java online submissions for Product of Array Except Self.
 *
 * Memory Usage: 42.8 MB, less than 48.03% of Java online submissions for Product of Array Except Self.
 *
 * @author D瓜哥 · https://www.diguage.com
 * @since 2020-01-05 20:15
 */
public int[] productExceptSelf(int[] nums) {
    int[] result = new int[nums.length];
    result[0] = 1;
    for (int i = 1; i < nums.length; i++) {
        result[i] = nums[i - 1] * result[i - 1];
    }
    int temp = 1;
    for (int i = nums.length - 1; i >= 0; i--) {
        result[i] *= temp;
        temp *= nums[i];
    }
    return result;
}

/**
 * Runtime: 1 ms, faster than 100.00% of Java online submissions for Product of Array Except Self.
 *
 * Memory Usage: 42.8 MB, less than 48.03% of Java online submissions for Product of Array Except Self.
 */
public int[] productExceptSelfDivision(int[] nums) {
    int product = 1;
    int zero = nums.length;
    for (int num : nums) {
        if (num == 0) {
            zero++;
            continue;
        }
        product *= num;
    }

    int[] result = new int[nums.length];
    if (zero >= nums.length + 2) {
        return result;
    }
    for (int i = 0; i < nums.length; i++) {
        int num = nums[i];
        if (num == 0) {
            result[i] = product;
        } else {
            if (zero == nums.length + 1) {
                result[i] = 0;
            } else {
                result[i] = product / num;
            }
        }
    }
    return result;
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
 * @author D瓜哥 · https://www.diguage.com
 * @since 2024-09-14 15:53:49
 */
public int[] productExceptSelf(int[] nums) {
  // 可以把 pre 和 post 简化掉,直接在 result 上进行计算
  int[] pre = new int[nums.length];
  pre[0] = nums[0];
  for (int i = 1; i < nums.length; i++) {
    pre[i] = nums[i] * pre[i - 1];
  }
  int[] post = new int[nums.length];
  post[nums.length - 1] = nums[nums.length - 1];
  for (int i = nums.length - 2; i >= 0; i--) {
    post[i] = nums[i] * post[i + 1];
  }
  int[] result = new int[nums.length];
  for (int i = 0; i < nums.length; i++) {
    int preNum = 1;
    if (0 < i) {
      preNum = pre[i - 1];
    }
    int postNum = 1;
    if (i < nums.length - 1) {
      postNum = post[i + 1];
    }
    result[i] = preNum * postNum;
  }
  return result;
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
/**
 * @author D瓜哥 · https://www.diguage.com
 * @since 2025-10-22 19:31:52
 */
public int[] productExceptSelf(int[] nums) {
  // 先使用 result 存储前缀乘积
  int[] result = new int[nums.length];
  result[0] = nums[0];
  for (int i = 1; i < nums.length - 1; i++) {
    result[i] = result[i - 1] * nums[i];
  }
  int suffix = 1; // 利用变量 suffix 存放后缀乘积
  for (int i = nums.length - 1; i > 0; i--) {
    result[i] = result[i - 1] * suffix;
    suffix *= nums[i];
  }
  result[0] = suffix;
  return result;
}

该题的思路也适用于 724. Find Pivot Index