238_Product of Array Except Self
Level: medium
tag: array
Question
Idea
The output equals multiplication of elements on the left of nums[i] and on the right of nums[i].
We are going to use two loops, one from left to right, one from right to left.
When we loop from left to right, we record the product of all the elements on the left, called leftPro, then append leftPro to the output.
When we loop from right to left, we record the product of all the elements on the right, called rightPro, then multiply it to the original output.
Time complexity
since there are two loops over each element.
Space complexity
Python solution
Last updated