add: 除自身以外数组的乘积
This commit is contained in:
21
src/array/product-of-array-except-self.js
Normal file
21
src/array/product-of-array-except-self.js
Normal 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
|
||||
}
|
Reference in New Issue
Block a user