Given an integer
numRows
, return the first numRows of Pascal’s triangle.In Pascal’s triangle, each number is the sum of the two numbers directly above it as shown:
Example 1:
Input: numRows = 5
Output: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
Example 2:
Input: numRows = 1
Output: [[1]]
Constraints:
*
1 <= numRows <= 30
题目大意:
给定n行,产生n行的杨辉三角
解题思路:
用DP按照定义生成,其实类似于Fibonacci数列,不过是二维的,而不是一维。
解题步骤:
N/A
注意事项:
- 初始值为[1]
Python代码:
1 | def generate(self, numRows: int) -> List[List[int]]: |
算法分析:
时间复杂度为O(numRows2)
,空间复杂度O(1)