add: 除自身以外数组的乘积

This commit is contained in:
轶哥
2020-06-04 13:30:06 +08:00
committed by yi-ge
parent 4aa8eba735
commit 8da050ef53
3 changed files with 30 additions and 0 deletions

View File

@ -0,0 +1,21 @@
/**
* @param {number[]} nums
* @return {number[]}
*/
export const productExceptSelf = function (nums) {
const ans = []
for (let i = 0, len = nums.length; i < len; i++) {
let tmpL = 1
let tmpR = 1
for (let j = 0; j < len; j++) {
if (j < i) {
tmpL *= nums[j]
} else if (j > i) {
tmpR *= nums[j]
}
}
ans.push(tmpL * tmpR)
}
return ans
}