Pascal's Triangle

Easy~15 min

Given an integer n, return the first n rows of Pascal's triangle.

In Pascal's triangle:

  • Each row starts and ends with 1.
  • Every interior element is the sum of the two elements directly above it from the previous row.

For example, given n = 5, the triangle looks like:

    [1]
   [1,1]
  [1,2,1]
 [1,3,3,1]
[1,4,6,4,1]

Examples

Example 1
Input: n = 5
Output: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
Explanation: Each interior value is the sum of the two values above it.
Example 2
Input: n = 1
Output: [[1]]
Explanation: The first row is always just [1].
Example 3
Input: n = 3
Output: [[1],[1,1],[1,2,1]]
Explanation: Row 3: 1, then 1+1=2, then 1.

Constraints

  • 1 <= n <= 30
Code
Ctrl+EnterRun|Ctrl+⇧+EnterSubmit
Output

Run your code to see results

Use Cmd+Enter to run