LeetCode 238 Product of Array Except Self (Python)

Posted by 小明MaxMing on April 26, 2020

题目

Given an array nums of n integers where n > 1, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].

解题思路

代码

class Solution:
    def productExceptSelf(self, nums: List[int]) -> List[int]:
        l = len(nums)
        res = [1] * l
        # l -> r
        for i in range(1, l):
            res[i] = res[i - 1] * nums[i - 1]
        # r -> l
        productR = 1
        for i in range(l-1, -1, -1):
            res[i] *= productR
            productR *= nums[i]
        return res

视频讲解 YouTube<--欢迎点击订阅

视频讲解 bilibili<--欢迎点击订阅