美文网首页python刷leetcode简单题
【leetcode】118、Pascal's Triangle

【leetcode】118、Pascal's Triangle

作者: 潇湘demi | 来源:发表于2018-02-28 16:32 被阅读0次

参考答案巧妙方法:

想得到第四行,第三行后加【0】和前加【0】想加,即

[1,2,1,0]+

[0,1,2,1]=

[1,3,3,1]

第1行:[[1]]

第2行:map(lambda x,y:x+y,[1]+[0],[0]+[1]) = [1,1]

第3行 : map(lambda x,y:x+y,[1,1]+[0],[0]+[1,1]) = [1,2,1]

def gen_pascal_trianle(nrows):

    if nrows ==0:

    return []

    res = [[1]]

    for iin range(1,nrows):

        res.append(map(lambda x,y:x+y,res[-1]+[0],[0]+res[-1]))

        print res

gen_pascal_trianle(8)

相关文章

网友评论

    本文标题:【leetcode】118、Pascal's Triangle

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