blob: 3b56f11abe3d9dcd107cab3c1e3f55c40d7a6250 (
plain)
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
|
import java.lang.*;
import java.util.*;
class ProductExceptSelf {
public static int[] product(int[] nums) {
int[] left = new int[nums.length];
int[] right = new int[nums.length];
left[0] = 1;
right[nums.length - 1] = 1;
int s = 1;
int e = nums.length - 2;
while (s <= nums.length && e >= 0) {
left[s] = left[s - 1] * nums[s - 1];
right[e] = right[e + 1] * nums[e + 1];
s++;
e--;
}
int[] res = new int[nums.length];
for (int i = 0; i < nums.length; i++)
res[i] = left[i] * right[i];
return res;
}
public static void main(String[] args) {
int[] nums = {-1, 1, 0, -3, 3};
System.out.println(Arrays.toString(product(nums)));
}
}
|