构建乘积数组 牛客网 剑指Offer
构建成绩数组 牛客网 剑指Offer
- 题目描述
- 给定一个数组A[0,1,...,n-1],请构建一个数组B[0,1,...,n-1],其中B中的元素B[i]=A[0]A[1]...*A[i-1]A[i+1]...*A[n-1]。不能使用除法
class Solution:
#run:29ms memory:5736k
def multiply(self,A):
if A == None or len(A) == 1:
return A
lenA = len(A)
lt = [1]*lenA
for i in range(1,lenA):
lt[i] = lt[i-1]*A[i-1]
temp = 1
for j in range(lenA-2,-1,-1):
temp *= A[j+1]
lt[j] *= temp
return lt