Skip to content

Commit efd1621

Browse files
committed
Product of Array Except Self
1 parent 0f4baad commit efd1621

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public int[] productExceptSelf(int[] nums) {
3+
int[] left = new int[nums.length + 1];
4+
int[] right = new int[nums.length + 1];
5+
left[0] = 1;
6+
right[nums.length] = 1;
7+
8+
for (int i = 0; i < nums.length; i++) {
9+
left[i + 1] = left[i] * nums[i];
10+
right[nums.length - i - 1] = right[nums.length - i] * nums[nums.length - i - 1];
11+
}
12+
13+
int[] result = new int[nums.length];
14+
for (int i = 0; i < nums.length; i++) {
15+
result[i] = left[i] * right[i + 1];
16+
}
17+
return result;
18+
}
19+
}

0 commit comments

Comments
 (0)