Skip to content

Commit 607512f

Browse files
committed
feat(soobing): product-of-array-except-self
1 parent 2d511d5 commit 607512f

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+
function productExceptSelf(nums: number[]): number[] {
2+
const left = Array(nums.length).fill(1);
3+
const right = Array(nums.length).fill(1);
4+
const result = Array(nums.length);
5+
6+
for (let i = 1; i < nums.length; i++) {
7+
left[i] = left[i - 1] * nums[i - 1];
8+
}
9+
10+
for (let i = nums.length - 2; i >= 0; i--) {
11+
right[i] = right[i + 1] * nums[i + 1];
12+
}
13+
14+
for (let i = 0; i < nums.length; i++) {
15+
result[i] = left[i] * right[i];
16+
}
17+
18+
return result;
19+
}

0 commit comments

Comments
 (0)