Wins the Offer (Python variety of ideas to achieve): Construction of the product array

Wins the Offer (Python variety of ideas to achieve): Construction of the product array

66 interview questions:

Title: Building product array

Given an array A [0,1, ..., n-1], please construct an array B [0,1, ..., n-1], where B is the element B [i] = A [ 0] * A [1] * ... * A [i-1] * A [i + 1] * ... * A [n-1]. You can not use the division.

Problem-solving ideas:

class Solution:
    def multiply(self, A):
        # write code here
        n=len(A)
        C=[1]*len(A)
        D=[1]*len(A)
        B=[1]*len(A)
        for i in range(1,n):
            C[i]=C[i-1]*A[i-1]
        for j in (range(0,n-1))[::-1]:
            D[j]=D[j+1]*A[j+1]
        
        for k in range(0,n):
            B[k]=C[k]*D[k]
        return B

 

Published 75 original articles · won praise 7 · views 40000 +

Guess you like

Origin blog.csdn.net/weixin_44151089/article/details/104549468