118 Pascal's Triangle

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 as shown:

    1
   1 1
  1 2 1
 1 3 3 1
1 4 6 4 1

Example 1:

Input: numRows = 5
Output: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]

Example 2:

Input: numRows = 1
Output: [[1]]

Constraints:

  • 1 <= numRows <= 30
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class Solution:
    def __init__(self):
        self.rows = [[1]]
        for i in range(29):
            prev_now = self.rows[-1]
            new_row = [prev_now[0]]
            for i in range(1, len(prev_now)):
                new_row.append(prev_now[i-1] + prev_now[i])
            new_row.append(prev_now[-1])
            self.rows.append(new_row)
    
    def generate(self, numRows: int) -> List[List[int]]:
        return self.rows[:numRows]