【leetcode】Pascal's Triangle

题目简述:

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]
]

解题思路:

很简单的问题,只要把两头的一拿出来,中间就是两个数相加了。

class Solution:
    # @return a list of lists of integers
    def generate(self, numRows):
        if numRows == 0:
            return []
        res = [[1]]
        for i in range(1,numRows):
            t = [1]
            pre = res[-1]
            for i in range(len(pre)-1):
                t.append(pre[i]+pre[i+1])
            t.append(1)
            res.append(t)
        return res
posted @ 2015-03-26 19:21  mrbean  阅读(225)  评论(0编辑  收藏  举报