238. Product of Array Except Self

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

Solve it without division and in .

For example, given [1,2,3,4], return [24,12,8,6].

Follow-Up:

Could you solve it with constant extra space?

Solution:

class Solution(object):
    def productExceptSelf(self, nums):
        """
        :type nums: List[int]
        :rtype: List[int]
        """
        size = len(nums)
        products = [1] * size
        product = 1
        for idx in xrange(1, size):
            product *= nums[idx - 1]
            products[idx] = product
        product = 1
        for idx in xrange(size - 2, -1, -1):
            product *= nums[idx + 1]
            products[idx] *= product
        return products

results matching ""

    No results matching ""