题目
Given an array nums of n integers where n > 1, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].
解题思路
代码
class Solution:
def productExceptSelf(self, nums: List[int]) -> List[int]:
l = len(nums)
res = [1] * l
# l -> r
for i in range(1, l):
res[i] = res[i - 1] * nums[i - 1]
# r -> l
productR = 1
for i in range(l-1, -1, -1):
res[i] *= productR
productR *= nums[i]
return res