Python Solution O(N) time, O(1) space


  • 0
    Y
    def get_products_of_all_ints_except_at_index(nums):
        result = [None] * len(nums)
        product = 1
        
        for i, num in enumerate(nums):
            result[i] = product
            product *= num
    
        product = 1
        for i in range(len(nums) - 1, -1, -1):
            result[i] *= product
            product *= nums[i]
        
        return result
    

Log in to reply
 

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