nathanwailes

LeetCode 238 - 2022.03.28 solution

Mar 29th, 2023
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.53 KB | None | 0 0
  1. class Solution:
  2.     def productExceptSelf(self, nums: List[int]) -> List[int]:
  3.         """
  4.  
  5.        [4, 7, 3]
  6.        [1, 4, 28] <-- left
  7.        [21, 3, 1] <-- right products
  8.        []
  9.        """
  10.         left_products = [1]
  11.         for i in range(1, len(nums)):
  12.             left_products.append(left_products[i-1] * nums[i-1])
  13.        
  14.         right_product = nums[-1]
  15.         for i in range(len(nums)-2, -1, -1):
  16.             left_products[i] *= right_product
  17.             right_product *= nums[i]
  18.         return left_products
Advertisement
Add Comment
Please, Sign In to add comment