109. Pascal's Triangle
Easy · Array
Given a non-negative integer numRows, generate the first numRows of Pascal's Triangle.
Pascal's Triangle is a triangular array of numbers where each row starts and ends with 1, and each interior element is the sum of the two elements above it from the previous row. The triangle has applications in combinatorics, probability, and algebra.
Examples
Example 1 Input: numRows = 5 Output: [[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1]] Explanation: The first 5 rows of Pascal's Triangle. Row 0 is [1]. Row 1 is [1, 1]. Row 2 is [1, 2, 1] where 2 = 1 + 1 from the row above. Row 3 is [1, 3, 3, 1] where 3 = 1 + 2 and 3 = 2 + 1. Row 4 is [1, 4, 6, 4, 1] where 4 = 1 + 3, 6 = 3 + 3, and 4 = 3 + 1.
Example 2 Input: numRows = 1 Output: [[1]] Explanation: With only 1 row requested, return a triangle containing just the single row [1].
Constraints
- Standard input/output constraints apply