【力扣100】238.除自身以外数组的乘积

添加链接描述

class Solution:
    def productExceptSelf(self, nums: List[int]) -> List[int]:
        # 构造第i个数的左右数组
        n = len(nums)
        left,right,res = [1]*n,[1]*n,[1]*n
        for i in range(1,n):
            left[i] = nums[i-1]*left[i-1]
        for i in range(n-2,-1,-1):
            right[i] = nums[i+1]*right[i+1]
        for i in range(n):
            res[i] = left[i]*right[i]
        return res

思路:

  1. 左边一个“前缀和”,右边一个“前缀和”
  2. 然后相乘
  3. 这个数组下标直接把我搞晕了老铁