题目描述:
题解:
1.product1初始化为1,用于保存前缀积,product1 = a[0]*a[1]*...*a[i-1]
2.product2初始化为1,用于保存后缀积,product2 = a[i+1]*a[i+2]*...*a[n-1]
3.先将product1赋值给b[i] 其中b[0]=1,的到前缀积,然后再将product2中的后缀积和b[i]相乘。
class Solution(object): def constructArr(self, a): n = len(a) b = [0]*n product1 = 1 for i in range(n): b[i] = product1 product1 = product1*a[i] product2 = 1 for i in range(n-1,-1,-1): b[i] = b[i]*product2 product2 = product2*a[i] return b