Untitled

 avatar
unknown
plain_text
10 months ago
679 B
6
Indexable
class Solution:
    def productExceptSelf(self, nums: List[int]) -> List[int]:
        prefixProduct, suffixProduct, sol = [], [], []
        acc = 1
        for num in nums:
            acc *= num
            prefixProduct.append(acc)

        acc = 1
        for num in nums[::-1]:
            acc *= num
            suffixProduct.append(acc)
        
        suffixProduct.reverse()
        sol.append(suffixProduct[1])
        
        for i in range(1, len(nums) - 1):
            product = prefixProduct[i - 1] * suffixProduct[i + 1]
            sol.append(product)
        
        sol.append(prefixProduct[len(nums) - 2])
        return sol




Editor is loading...
Leave a Comment