-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuildProductArray.java
More file actions
22 lines (21 loc) · 652 Bytes
/
BuildProductArray.java
File metadata and controls
22 lines (21 loc) · 652 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package leetcode.array;
/**
* @ClassName BuildProductArray
* @Description 构建乘积数组 https://leetcode-cn.com/problems/gou-jian-cheng-ji-shu-zu-lcof/
* @Author changxuan
* @Date 2020/10/9 下午8:32
**/
public class BuildProductArray {
public int[] constructArr(int[] a) {
if (a.length == 0) return new int[0];
int[] res = new int[a.length];
res[0] = 1;
int temp = 1;
for (int i = 1; i < a.length; i++) res[i] = a[i - 1] * res[i - 1];
for (int i = a.length - 2; i >= 0; i--) {
temp = temp * a[i + 1];
res[i] = res[i] * temp;
}
return res;
}
}