Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def productExceptSelf(self, nums: List[int]) -> List[int]:
- """ Solution: Have one list of the products going from R->L.
- Then work through the list, just keeping a variable for
- the L->R product.
- Then, for each index, multiply the value at i in the R->L list
- with the variable storing the L->R product.
- """
- r_to_l = [None for n in nums]
- for i in range(len(nums)-1, -1, -1):
- if i == len(nums)-1:
- r_to_l[i] = 1
- else:
- r_to_l[i] = r_to_l[i+1] * nums[i+1]
- l_to_r = 1
- products = []
- for i in range(len(nums)):
- products.append(l_to_r * r_to_l[i])
- l_to_r *= nums[i]
- return products
Advertisement
Add Comment
Please, Sign In to add comment