Simple Python O(n) Time O(1) Space


  • 0

    One sweep. Still would be considered O(2n) as it's basically doing a sweep both ways but since O(2n) is considered O(n) it's all good.

    class Solution(object):
        def productExceptSelf(self, nums):
            """
            :type nums: List[int]
            :rtype: List[int]
            """
            if not nums: return []
            rList = [1] * len(nums)
            i, fp, bp = 0, 1, 1
            for i in range(len(nums)):
                calc = len(nums)-1-i
                rList[i] *= fp;  rList[calc] *= bp
                fp *= nums[i]; bp *= nums[calc]
            return rList
    

Log in to reply
 

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