Product of Array Except Self

从两边分别遍历一遍就能解决了

class Solution(object):
    def productExceptSelf(self, nums):
        """
        :type nums: List[int]
        :rtype: List[int]
        """
        cumMul = 1
        n = len(nums)
        result = [1] * n

        for i in xrange(n):
            result[i] *= cumMul
            cumMul *= nums[i]

        cumMul = 1
        for i in xrange(n - 1, -1, -1):
            result[i] *= cumMul
            cumMul *= nums[i]

        return result

results matching ""

    No results matching ""