Skip to content

Commit df0f584

Browse files
committed
product-of-array-except-self
1 parent 03c0ff6 commit df0f584

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

0 commit comments

Comments
 (0)