Given an integer num_rows, return the first num_rows rows of Pascal's triangle as a list of integer lists.
The triangle starts with the row [1]. Each row begins and ends with 1, and each interior value is the sum of the two values immediately above it.
All values in the result fit within a signed 32-bit integer.
Function Signature
def generate(num_rows: int) -> list[list[int]]:
Rules
-
Row
i
(counting from 0) has
i + 1
values. For
0 < j < i
, its value at position
j
is
triangle[i - 1][j - 1] + triangle[i - 1][j]
.
-
Return the rows from top to bottom, with the values of each row in left-to-right order.
Constraints
Examples
Input: num_rows = 5
Output: [[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1]]
Each interior value is the sum of the two values directly above it. In the last row, 4 = 1 + 3, 6 = 3 + 3 and 4 = 3 + 1.
Input: num_rows = 1
Output: [[1]]
Only the first row [1] is returned.
Input: num_rows = 2
Output: [[1], [1, 1]]
The second row has no interior positions, so it holds only the two edge values.