A simple python solution which is easy to understand


  • 0
    H
    class Solution(object):
        def productExceptSelf(self, nums):
            if len(nums)==1:
                return nums[0]
            temp=[]
            static=[]
            temp.append(1)
            static.append(1)
            i=1
            j=len(nums)-2
            while j>=0 and i<len(nums):
                temp.append(temp[i-1]*nums[i-1])
                static.append(static[i-1]*nums[j+1])
                i+=1
                j-=1
            ans=[]
            n=len(nums)
            for i in range(n):
                ans.append(temp[i]*static[n-1-i])
            return ans
    

Log in to reply
 

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