Pascal's Triangle

来源:互联网 发布:icloud删除的数据恢复 编辑:程序博客网 时间:2024/06/07 02:52

Given numRows, generate the first numRows of Pascal’s triangle.

For example, given numRows = 5,
Return

[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]

自己没有想到很好的解法,看了solution,被其代码惊呆了,写的很好。

class Solution(object):    def generate(self, numRows):        """        :type numRows: int        :rtype: List[List[int]]        """        if numRows == 0:            return []        res = [[1]]        for i in range(1, numRows):            res += [map(lambda x, y: x+y, res[-1] + [0], [0] + res[-1])]        return res
0 0
原创粉丝点击