dsa · easy
Pascal's Triangle
Build the first num_rows rows of Pascal’s triangle and return them as a list of rows. Row 0 is [1]. Each later row starts and ends with 1, and every interior entry is the sum of the two entries above it from the previous row.
Arguments
num_rows— how many rows to generate, counting from the top row
Example
num_rows = 5.
Row 0: [1]
Row 1: [1, 1]
Row 2: the interior 1 + 1 = 2 → [1, 2, 1]
Row 3: interiors 1+2=3, 2+1=3 → [1, 3, 3, 1]
Row 4: interiors 1+3=4, 3+3=6, 3+1=4 → [1, 4, 6, 4, 1].
Return those five rows in that order.
Constraints
1 <= num_rows <= 30
Examples
Example 1
Input: 5 Expected: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
Example 2
Input: 1 Expected: [[1]]