Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

238-除自身以外数组的乘积 #10

Open
YBFACC opened this issue Jun 4, 2020 · 0 comments
Open

238-除自身以外数组的乘积 #10

YBFACC opened this issue Jun 4, 2020 · 0 comments

Comments

@YBFACC
Copy link
Owner

YBFACC commented Jun 4, 2020

238-除自身以外数组的乘积

我的思路

从左扫一遍,累乘得到一个left数组

从右扫一遍,累乘得到一个right数组

把nums [ i ] 的left [ i -1 ] * right [ i +1 ]得到答案数组

未命名文件

代码实现

var productExceptSelf = function (nums) {
  let left = [1]

  for (let i = 0; i < nums.length; i++) {
    left[i + 1] = left[i] * nums[i]
  }
  left.push(1)

  let right = [1]
  right[nums.length + 1] = 1
  for (let i = nums.length; i > 0; i--) {
    right[i] = right[i + 1] * nums[i - 1]
  }

  let res = []

  for (let i = 0; i < left.length - 2; i++) {
    res[i] = left[i] * right[i + 2]
  }

  return res
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant