LintCode_chapter2_section5_product-of-array-exclude-itself

# coding = utf-8
'''
Created on 2015年11月9日

@author: SphinxW
'''
# 数组剔除元素后的乘积
#
# 给定一个整数数组A。
#
# 定义B[i] = A[0] * ... * A[i-1] * A[i+1] * ... * A[n-1], 计算B的时候请不要使用除法。
# 样例
#
# 给出A=[1, 2, 3],返回 B为[6, 3, 2]


class Solution:
    """
    @param A: Given an integers array A
    @return: An integer array B and B[i]= A[0] * ... * A[i-1] * A[i+1] * ... * A[n-1]
    """

    def productExcludeItself(self, A):
        # write your code here
        length = len(A)
        B = [1 for index in A]
        for index in range(length):
            for indexA, itemA in enumerate(A):
                if indexA == index:
                    pass
                else:
                    B[index] = B[index] * A[indexA]
        return B

你可能感兴趣的:(LintCode_chapter2_section5_product-of-array-exclude-itself)