Skip to content

Commit 9440022

Browse files
committed
Create 238.除自身以外数组的乘积.js
1 parent cd864f8 commit 9440022

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

238.除自身以外数组的乘积.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number[]}
4+
*/
5+
var productExceptSelf = function(nums) {
6+
const a = [nums[0]];
7+
const b = [nums[nums.length - 1]];
8+
for (let i = 1; i < nums.length; i++) {
9+
a.push(a[i - 1] * nums[i]);
10+
b.push(b[i - 1] * nums[nums.length -1 - i]);
11+
}
12+
return nums.map((n, i) => {
13+
return (a[i - 1] === undefined ? 1 : a[i - 1]) * (b[b.length - 2 - i] === undefined ? 1 : b[b.length - 2 - i]);
14+
})
15+
};

0 commit comments

Comments
 (0)