-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
49 lines (42 loc) · 1.24 KB
/
Solution.java
File metadata and controls
49 lines (42 loc) · 1.24 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
43
44
45
46
47
48
49
package array.prodnoself;
/**
* @see <a href="https://leetcode.com/problems/product-of-array-except-self">Product of Array Except Self</a>
*/
public class Solution {
/**
* Solution #1
*
* Clever implementation with O(n) time complexity and space complexity O(1) if not counting the result array.
*/
public int[] productExceptSelf(int[] nums) {
int[] result = new int[nums.length];
result[0] = 1;
for (int i = 1; i < nums.length; i++) {
result[i] = result[i - 1] * nums[i - 1];
}
int right = 1;
for (int i = nums.length - 1; i >= 0; i--) {
result[i] = result[i] * right;
right = right * nums[i];
}
return result;
}
/**
* Solution #2
*
* Brute force implementation with O(n^2) time complexity.
*/
public int[] productExceptSelfBruteForce(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++) {
if (i != j) {
product *= nums[j];
}
}
result[i] = product;
}
return result;
}
}