smj007

Untitled

Jun 18th, 2022 (edited)
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.49 KB | None | 0 0
  1. class Solution:
  2.     def productExceptSelf(self, nums: List[int]) -> List[int]:
  3.        
  4.         # create a prefix and suffix product arrays
  5.         product = [1]*len(nums)
  6.        
  7.         prefix, suffix = 1, 1
  8.         for i in range(len(nums)):
  9.             product[i] = prefix
  10.             prefix *= nums[i]
  11.          
  12.         for i in range(len(nums) - 1, -1, -1):
  13.             product[i] = product[i] * suffix
  14.             suffix = suffix * nums[i]
  15.            
  16.         return product
Add Comment
Please, Sign In to add comment