118. Pascal’s Triangle

Information

  • Diffculty: Easy

  • Created: 2020-03-11 07:51:00

  • Last Motified: 2020-03-11 07:58:10

  • Tags: 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