-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ238.java
More file actions
28 lines (26 loc) · 707 Bytes
/
Q238.java
File metadata and controls
28 lines (26 loc) · 707 Bytes
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
/*
* @Author: Shawn Yang
* @Date: 2019-09-05 20:51:06
* @Last Modified by: Shawn Yang
* @Last Modified time: 2019-09-05 20:51:12
*/
class Solution {
public int[] productExceptSelf(int[] nums) {
if(nums == null || nums.length == 0) {
return null;
}
int[] result = new int[nums.length];
int left = 1;
result[0] = left;
for(int i = 1; i < nums.length; i++){
left = left * nums[i - 1];
result[i] = left;
}
int right = 1;
for(int j = nums.length - 1; j >= 0; j--) {
result[j] = result[j] * right;
right = nums[j] * right;
}
return result;
}
}