【leetcode】Pascal's Triangle

題目簡述:

Given numRows, generate the first numRows of Pascal's triangle.app

For example, given numRows = 5,
Returncode

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

解題思路:

很簡單的問題,只要把兩頭的一拿出來,中間就是兩個數相加了。class

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
相關文章
相關標籤/搜索