Skip to content

Commit fb3974b

Browse files
committed
product-of-array-except-self solved
1 parent a4874d3 commit fb3974b

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number[]}
4+
*/
5+
var productExceptSelf = function(nums) {
6+
const n = nums.length
7+
const result = Array(nums.length)
8+
9+
let left = 1
10+
11+
for(let i = 0; i < n; i++) {
12+
result[i] = left
13+
left *= nums[i]
14+
}
15+
16+
let right = 1
17+
for(let i = n - 1; i >= 0; i--) {
18+
result[i] *= right
19+
right *= nums[i]
20+
}
21+
22+
return result
23+
};

0 commit comments

Comments
 (0)