Skip to content

Commit 51ed2be

Browse files
committed
feat: product-of-array-except-self solution
1 parent 536fe6a commit 51ed2be

File tree

1 file changed

+18
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)