【leetcode】Pascal's Triangle

时间:2023-12-28 17:08:26

题目简述:

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