LeetCode_Pascal's Triangle

Pascal’s Triangle

Given a non-negative integer numRows, generate the first numRows of Pascal’s triangle.
(杨辉三角)

Example:



1. 逐层生成

根据杨辉三角的规律,组成生成杨辉三角,具体实现过程如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution:
def generate(self, numRows: int) -> List[List[int]]:
if numRows == 0:
return []

results = []
for i in range(numRows):
if i == 0:
results.append([1])
else:
list = [1]
for j in range(i-1):
list.append(results[i-1][j] + results[i-1][j+1])
list.append(1)
results.append(list)

return results