构建乘积数组

题目描述
给定一个数组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]。不能使用除法。

分别求上三角和下三角乘积

# -*- coding:utf-8 -*-
class Solution:
    def multiply(self, A):
        # write code here
        l1, l2 = [1]*len(A), [1]*len(A)
        for i in range(1, len(A)):
            l1[i] = l1[i-1]*A[i-1]
        for i in range(len(A)-2, -1, -1):
            l2[i] = l2[i+1]*A[i+1]
        res = [0]*len(A)
        
        for i in range(len(A)):
            res[i] = l1[i]*l2[i]
        return res
    

你可能感兴趣的:(构建乘积数组)