Loading...

09-08 leetcode-0118

链接 118. Pascal’s Triangle

题目

Given an integer numRows, return the first numRows of Pascal’s triangle.

题解

某种意义上算一个最开始的 DP ?我猜(

1
2
3
4
5
6
7
8
9
10
11
12
class Solution:
def generate(self, numRows: int) -> list[list[int]]:
result = [[1], [1, 1]]
if numRows < 3:
return result[:numRows]
for i in range(2, numRows):
result.append(
[1]
+ [result[i - 1][j] + result[i - 1][j + 1] for j in range(i - 1)]
+ [1]
)
return result

Comment