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.
Algorithm
Dynamic Programming (DP). F(i, j) = F(i-1, j-1) + F(i-1, j).
Code
class Solution:
def generate(self, numRows: int) -> List[List[int]]:
ans = [[1]]
for i in range(1, numRows):
buf = [1]
for j in range(1, i):
buf.append(ans[i-1][j-1] + ans[i-1][j])
buf.append(1)
ans.append(buf)
return ans