Python One Pass Solution O(n) time and no extra space


  • 0
    I
    def productExceptSelf(self, nums):
        """
        :type nums: List[int]
        :rtype: List[int]
        """
        res=[1 for _ in nums]
        pl,pr,l=1,1,len(nums)
        for i in range(l):
            res[i]*=pl
            res[l-1-i]*=pr
            pl*=nums[i]
            pr*=nums[l-1-i]
        return res

Log in to reply
 

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