nathanwailes

LeetCode 238 - Product of Array Except Self - NeetCode solution

Oct 5th, 2023
852
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.37 KB | None | 0 0
  1. class Solution:
  2.     def productExceptSelf(self, nums: List[int]) -> List[int]:
  3.         res = [1] * (len(nums))
  4.  
  5.         prefix = 1
  6.         for i in range(len(nums)):
  7.             res[i] = prefix
  8.             prefix *= nums[i]
  9.         postfix = 1
  10.         for i in range(len(nums) - 1, -1, -1):
  11.             res[i] *= postfix
  12.             postfix *= nums[i]
  13.         return res
Advertisement
Add Comment
Please, Sign In to add comment