构建乘积数组 牛客网 剑指Offer

mac2022-06-30  94

构建成绩数组 牛客网 剑指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

 

转载于:https://www.cnblogs.com/vercont/p/10210366.html

最新回复(0)