Pascal's Triangle

Problem Id: 118 Difficulty: Easy Tag: Array


Intuition

This is a simple problem. Just calculate the numbers by top-down order.

Solution


class Solution:
    def generate(self, numRows: int) -> List[List[int]]:
        if numRows == 0:
            return []
        data = [[1]]
        for depth in range(2, numRows + 1):
            row = [1] * depth
            for i in range(1, depth - 1):
                row[i] = data[-1][i] + data[-1][i - 1]
            data.append(row)
        return data