Участник:Ковальков Антон М05-903б/Product of Array Except Self

Материал из DISCOPAL
Перейти к: навигация, поиск

https://leetcode.com/problems/product-of-array-except-self/

Python3

class Solution:
    def productExceptSelf(self, nums: List[int]) -> List[int]:
        res = [0] * len(nums)
        res[-1] = nums[-1]
        for i in range(len(nums)-1)[::-1]:
            res[i] = res[i+1] * nums[i]
        l = 1
        i = 0
        while True:
            if i == len(nums) - 1:
                res[i] = l
                return res
            res[i] = l * res[i + 1]
            l *= nums[i]
            i += 1