動機
水題
Problem
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 = 5Output: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
Example 2:
Input: numRows = 1Output: [[1]]
Constraints:
1 <= numRows <= 30
Sol
class Solution:
def generate(self, n: int) -> List[List[int]]:
if n == 1:
return [[1]]
else:
ret = self.generate(n-1)
ret += [[ret[-1][0]] + [ret[-1][i] + ret[-1][i+1] for i in range(len(ret[-1])-1)] + [ret[-1][-1]]]
return ret