美文网首页
LintCode_chapter2_section5_produ

LintCode_chapter2_section5_produ

作者: 穆弋 | 来源:发表于2015-11-09 14:24 被阅读8次
# 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_produ

      本文链接:https://www.haomeiwen.com/subject/xoxhhttx.html