Python easy to understand solution


  • 0
    P
    class Solution(object):
        def productExceptSelf(self, nums):
            """
            :type nums: List[int]
            :rtype: List[int]
            """
            # compute the products of elements on the left (from left to right)
            left = [1] * len(nums)
            for i in range( 1, len(nums) ):
                left[i] = left[i-1]*nums[i-1]
            
           # compute the products of elements on the right(from right to left)
            right = [1] * len(nums)
            for i in range(len(nums)-2, -1, -1):
                right[i] = right[i+1] * nums[i+1]
            
           # 
            res = []
            for i,j in zip(left, right):
                res.append(i*j)
            return res
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.