Skip to content

Latest commit

 

History

History
35 lines (28 loc) · 1.02 KB

66. 构建乘积数组.md

File metadata and controls

35 lines (28 loc) · 1.02 KB

题目链接:

剑指 Offer 66. 构建乘积数组

思路:

  1. 求前缀和的思路一致,分别求出左累积数组右累积数组
  2. 两个数组的元素对应相乘,即可得到当前元素的答案

代码:

JavaScript

const constructArr = a => {
    const len = a.length;
    // 定义左累积数组、右累积数组,初始值都为 1
    const [resLeft, resRight] = [new Array(len).fill(1), new Array(len).fill(1)];
    for (let i = 1; i < len; i++) {
        // 从i=1开始,作左累积运算
        resLeft[i] = resLeft[i - 1] * a[i - 1];
    }
    for (let i = len - 2; i >= 0; i--) {
        // 从i=len - 2开始,作右累积运算
        resRight[i] = resRight[i + 1] * a[i + 1];
    }
    const res = [];
    for (let i = 0; i < len; i++) {
        // 两个数组的元素对应相乘,即可得到当前元素的答案
        res.push(resLeft[i] * resRight[i]);
    }
    return res;
};