-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathProductOfArrayExceptSelf.java
More file actions
40 lines (34 loc) · 1.08 KB
/
Copy pathProductOfArrayExceptSelf.java
File metadata and controls
40 lines (34 loc) · 1.08 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
/**
* 238. Product of Array Except Self
* Time Complexity: O(n^2)
* Space Complexity: O(n)
*/
public class ProductOfArrayExceptSelf {
public int[] productExceptSelf(int[] nums) {
int[] result = new int[nums.length];
for (int i = 0; i < nums.length; i++) {
int product = 1;
for (int j = 0; j < nums.length; j++) {
// for any i, calculate all j's except i
if (i == j) {
continue;
}
product = product * nums[j];
}
result[i] = product;
}
return result;
}
public static void main(String[] args) {
int[] result = new ProductOfArrayExceptSelf().productExceptSelf(new int[]{1, 2, 3, 4, 5});
for (int r : result) {
System.out.print(r + " ");
}
System.out.println("");
result = new ProductOfArrayExceptSelf().productExceptSelf(new int[]{-1, 1, 0, -3, 3});
for (int r : result) {
System.out.print(r + " ");
}
System.out.println("");
}
}