Easy understand two scans Python


  • 5
    C

    class Solution(object):

    def productExceptSelf(self, nums):
        """
        :type nums: List[int]
        :rtype: List[int]
        """
        
        res = [1]
        prev = 1
        #scan forward
        for i in xrange(1,len(nums)):
            prev=prev*nums[i-1]
            res.append(prev)
        #scan backward
        prev = 1
        for i in xrange(len(nums)-2,-1,-1):
            prev = prev*nums[i+1]
            res[i]=res[i]*prev
            
            
        return res

Log in to reply
 

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