Leetcode238 除自身以外数组的乘积

题目描述

image.png

方法一

class Solution:
    def productExceptSelf(self, nums: List[int]) -> List[int]:
        left, right,res = [1] * len(nums), [1] * len(nums),[1] * len(nums)
        for i in range(1,len(nums)):
            left[i] = left[i - 1] * nums[i - 1]
        for i in range(len(nums)-2,-1,-1):
            right[i] = right[i + 1] * nums[i + 1]
        for i in range(len(nums)):
            res[i] = left[i] * right[i]
        return res

 

Note:

  • 回溯法/DP ,左乘数组&右乘数组