class Solution:
def multiply(self, A:List):
B=[]
for i in range(len(A)):
tmp = 1
for i,n in enumerate(A):
if i!=len(B):tmp*=n
B.append(tmp)
return B
对连乘降低复杂度,10min
class Solution:
def multiply(self, A:List):
B=[]
a=[1 for i in range(len(A))]
b=[1 for i in range(len(A))]
for i in range(1,len(A)):
a[i]=a[i-1]*A[i-1]
b[len(A) - 1 - i] = b[len(A) - 1 - i + 1] * A[len(A) - 1 - i+1]
for i in range(len(A)):
B.append(a[i]*b[i])
return B
网友评论