Preview:
# NC version -- ponder on this more! 
class Solution:
    def productExceptSelf(self, nums: List[int]) -> List[int]:
        res = [1] * (len(nums))

        for i in range(1, len(nums)):
            res[i] = res[i-1] * nums[i-1]
        postfix = 1
        for i in range(len(nums) - 1, -1, -1):
            res[i] *= postfix
            postfix *= nums[i]
        return res

# my version 
class Solution:
    def productExceptSelf(self, nums: List[int]) -> List[int]:
        
        pre = [1]+nums[:]+[1]
        for i in range(1, len(pre)-1):
            pre[i] = pre[i-1]*pre[i]
        print(pre)
 
        post = [1]+nums[:]+[1]
        for i in range(len(post)-2, 0, -1):
            post[i] = post[i+1]*post[i]
        print(post)
        
        res = []
        for i in range(1, len(nums)+1):
            res.append(pre[i-1]*post[i+1])
        return res
downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter