[leetcode-python3] 238. Product of Array Except Self

903 ワード

238. Product of Array Except Self - python3


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].

Solution 1: Accepted (Runtime: 240 ms - 57.91% / Memory Usage: 20.9 MB - 81.82%)

class Solution:
    def productExceptSelf(self, nums: List[int]) -> List[int]:
        result = [1] * len(nums)        
        for i in range(1, len(nums)):
            result[i] = nums[i-1] * result[i-1]
            
        right_prod = 1
        for i in range(len(nums)-1, -1, -1):
            result[i] *= right_prod
            right_prod *= nums[i]             
        
        return result
Python, no division, time: O(N), space: O(1) (not including the output array)
これは遡及とあまり差がありませんか...作りましたが、ダメなので、そのまま切りましょう.
何を言ってるんだ?