-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLeetCode0238.java
53 lines (43 loc) · 1.49 KB
/
LeetCode0238.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
47
48
49
50
51
52
53
/* Product of Array Except Self
* Input: [1,2,3,4]
* Output: [24,12,8,6]
* Note: Please solve it without division and in O(n).
* Follow up: Could you solve it with constant space complexity?
* */
public class LeetCode0238 {
public static void main(String args[]) {
int[] nums = {1, 2, 3, 4};
int[] res = productExceptSelf(nums);
for (int num : res)
System.out.print(num + ",");
}
public static int[] productExceptSelf(int[] nums) {
/* Time complexity: O(N), Space complexity : O(N)
int length = nums.length;
int[] left = new int[length];
int[] right = new int[length];
int[] res = new int[length];
left[0] = 1;
for (int i = 1; i < length; i++)
left[i] = left[i - 1] * nums[i - 1];
right[length - 1] = 1;
for (int i = length - 2; i >= 0; i--)
right[i] = right[i + 1] * nums[i + 1];
for (int i = 0; i < length; i++) {
res[i] = left[i] * right[i];
}
return res;*/
// Time complexity: O(N), Space complexity : O(1)
int length = nums.length;
int[] res = new int[length];
res[0] = 1;
for (int i = 1; i < length; i++)
res[i] = res[i - 1] * nums[i - 1];
int right = 1;
for(int i = length - 1; i >= 0; i--){
res[i] = res[i] * right;
right *= nums[i];
}
return res;
}
}