silver-hands / sss

0 stars 0 forks source link

【Q065】面试题66. 构建乘积数组 #65

Open fly0o0 opened 4 years ago

fly0o0 commented 4 years ago

面试题66. 构建乘积数组

fly0o0 commented 4 years ago
/**
 * @param {number[]} a
 * @return {number[]}
 */
var constructArr = function(a) {
  let n = a.length

  let left = 1
  let ret = []

  for (let i = 0; i < n; i++) {
    ret[i] = left
    left *= a[i]
  }

  let right =  1

  for (let j = n - 1; j >= 0; j--) {
    ret[j] *= right
    right *= a[j]
  }
  return ret
};