剑指offer66 构建乘积数组【辅助数组】

题目描述

给定一个数组 A[0,1,…,n-1],请构建一个数组 B[0,1,…,n-1],其中 B[i] 的值是数组 A 中除了下标 i 以外的元素的积, 即 B[i]=A[0]×A[1]×…×A[i-1]×A[i+1]×…×A[n-1]。不能使用除法。

示例:

1
2
输入: [1,2,3,4,5]
输出: [120,60,40,30,24]

提示:

  • 所有元素乘积之和不会溢出 32 位整数
  • a.length <= 100000

解题思路

方法一:辅助数组

由于题目给出不能使用除法操作,那么可以直接先计算出左边的乘积数组和右边的乘积数组。最后将两个数组相乘得出最终结果。

代码实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public int[] constructArr(int[] a) {
int len = a.length;
if (len == 0) {
return new int[0];
}
int[] left = new int[len];
int[] right = new int[len];
int[] res = new int[len];
left[0] = 1;
right[len - 1] = 1;
for (int i = 1; i < len; i++) {
left[i] = left[i - 1] * a[i - 1];
}
for (int i = len - 2; i >= 0; i--) {
right[i] = right[i + 1] * a[i + 1];
}
for (int i = 0; i < len; i++) {
res[i] = left[i] * right[i];
}
return res;
}

复杂度分析

时间复杂度:O(n)

空间复杂度:O(n)

优化代码实现

优化空间,将left,right数组用一个变量来替代,节省空间开销

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public int[] constructArr(int[] a) {
int len = a.length;
if (len == 0) {
return new int[0];
}
int[] res = new int[len];
int temp = 1;
// 先将left乘积存入res数组中
for (int i = 0; i < len; i++) {
res[i] = temp;
temp = temp * a[i];
}
// 再将right数组乘以res数组。
temp = 1;
for (int i = len - 1; i >= 0; i--) {
res[i] = res[i] * temp;
temp = temp * a[i];
}
return res;
}

复杂度分析

时间复杂度:O(n)

空间复杂度:O(1) // 除答案使用空间

资料

剑指offer66 构建乘积数组【辅助数组】

http://example.com/2021/06/06/剑指offer66-构建乘积数组/

Author

John Doe

Posted on

2021-06-06

Updated on

2021-06-08

Licensed under

You need to set install_url to use ShareThis. Please set it in _config.yml.
You forgot to set the business or currency_code for Paypal. Please set it in _config.yml.

Comments

You forgot to set the shortname for Disqus. Please set it in _config.yml.