难度中等142
给定一个数组 A[0,1,…,n-1],请构建一个数组 B[0,1,…,n-1],其中 B[i] 的值是数组 A 中除了下标 i 以外的元素的积, 即 B[i]=A[0]×A[1]×…×A[i-1]×A[i+1]×…×A[n-1]。不能使用除法。
示例:
输入: [1,2,3,4,5] 输出: [120,60,40,30,24]
class Solution:
def constructArr(self, a: List[int]) -> List[int]:
n=len(a)
dp=[1]*n
tmp=1
for i in range(1,n):
dp[i]=dp[i-1]*a[i-1] # 下三角
for i in range(n-2,-1,-1):
tmp=tmp*a[i+1] # 上三角
dp[i]=dp[i]*tmp # 下三角 * 上三角
return dp
344



被折叠的 条评论
为什么被折叠?



