构建乘积数组
2023/2/11小于 1 分钟
构建乘积数组
题目链接
题目描述
刷题思路
代码实现
/**
* 【简单】构建乘积数组
*/
function multiply(array) {
const result = []
for (let index = 0; index < array.length; index++) {
// result
// .push(
// array
// .slice(0, index)
// .reduce((res, item) => res * item, 1) * array.slice(index + 1)
// .reduce((res, item) => res * item, 1)
// )
result.push(
[
...array.slice(0, index),
...array.slice(index + 1),
].reduce((res, item) => res * item, 1),
) // 给res的初始值为1
}
return result
}
console.log(multiply([1, 2, 3, 4, 5]))一些建议
更新日志
2024/7/29 15:43
查看所有更新日志
5a2b2-于c0f2d-于06596-于9b9e4-于b0275-于5f1e1-于02ab1-于8de1a-于74e84-于ced18-于a23ce-于e34c0-于74aa9-于3c22c-于9bbe9-于e4c74-于