-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode238.java
More file actions
42 lines (38 loc) · 1.19 KB
/
LeetCode238.java
File metadata and controls
42 lines (38 loc) · 1.19 KB
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
import java.util.Arrays;
public class LeetCode238 {
public static void main(String[] args) {
// 输入: nums = [1,2,3,4]
// 输出: [24,12,8,6]
System.out.println(Arrays.toString(new Solution238().productExceptSelf(new int[] { 1, 2, 3, 4 })));
// 输入: nums = [-1,1,0,-3,3]
// 输出: [0,0,9,0,0]
System.out.println(Arrays.toString(new Solution238().productExceptSelf(new int[] { -1, 1, 0, -3, 3 })));
}
}
class Solution238 {
public int[] productExceptSelf(int[] nums) {
int zeroCount = 0;
int prod = 1;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == 0) {
zeroCount++;
} else {
prod *= nums[i];
}
}
int[] result = new int[nums.length];
if (zeroCount >= 2) {
} else if (zeroCount == 1) {
for (int i = 0; i < result.length; i++) {
if (nums[i] == 0) {
result[i] = prod;
}
}
} else {
for (int i = 0; i < result.length; i++) {
result[i] = prod / nums[i];
}
}
return result;
}
}