友情支持

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

支付宝

微信

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

wx jikerizhi

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

238. Product of Array Except Self

Given an array nums of n integers where n > 1, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].

Example:

Input:  [1,2,3,4]
Output: [24,12,8,6]

Note: *Please solve it *without division and in O(n).

Follow up:

Could you solve it with constant space complexity? (The output array does not count as extra space for the purpose of space complexity analysis.)

思路分析

  • 一刷

  • 二刷

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

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