-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProductOfArrayExceptSelf.java
46 lines (38 loc) · 1.11 KB
/
ProductOfArrayExceptSelf.java
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
43
44
45
46
package array;
/**
* @author Shogo Akiyama
* Solved on 02/16/2020
*
* 238. Product of Array Except Self
* https://leetcode.com/problems/product-of-array-except-self/
* Difficulty: Medium
*
* Approach: Left And Right Product Lists
* Runtime: 1 ms, faster than 100.00% of Java online submissions for Product of Array Except Self.
* Memory Usage: 47.7 MB, less than 5.51% of Java online submissions for Product of Array Except Self.
*
* Time Complexity: O(n)
* Space Complexity: O(n)
* Where n is the number of elements in an array
*
* @see ArrayTest#testProductOfArrayExceptSelf()
*/
public class ProductOfArrayExceptSelf {
public int[] productExceptSelf(int[] nums) {
int[] front = new int[nums.length];
int[] back = new int[nums.length];
int[] result = new int[nums.length];
front[0] = 1;
back[nums.length - 1] = 1;
for (int i = 1; i < nums.length; i++) {
front[i] = front[i - 1] * nums[i - 1];
}
for (int i = nums.length - 2; i >= 0; i--) {
back[i] = back[i + 1] * nums[i + 1];
}
for (int i = 0; i < nums.length; i++) {
result[i] = front[i] * back[i];
}
return result;
}
}