Most basic approach in Python O(n)


  • 1
    A

    class Solution(object):
    def productExceptSelf(self, nums):

        output=[1]*len(nums)
        n=len(nums)
        
        prod=1
        for i in range(1,n):
            prod=prod*nums[i-1]
            output[i]*=prod
            
        prod=1
        for i in range(n-2,-1,-1):
            prod=prod*nums[i+1]
            output[i]*=prod
        
        return output

  • 0
    L

    Cool. Your answer is cool and easy to understand. Many assholes only vote to wrong answers without trial by themselves.


  • 0
    A

    @leefyi Thank you! :)


Log in to reply
 

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