Untitled

mail@pastecode.io avatar
unknown
python
a year ago
419 B
5
Indexable
class Solution:
    def productExceptSelf(self, nums: List[int]) -> List[int]:
        if not nums:
            return nums
        forwards = [1]
        backwards = [1]
        
        for i, j in zip(nums[:-1], reversed(nums[1:])):
            forwards.append(forwards[-1]*i)
            backwards.append(backwards[-1]*j)
        
        return [forwards[i]*backwards[len(backwards)-1-i] for i in range(len(nums))]