Given a non-negative integer numRows, generate the first numRows of Pascal’s triangle.

file

In Pascal’s triangle, each number is the sum of the two numbers directly above it.

Example:
Input: 5
Output:

Solution in python:

class Solution:
    def generate(self, numRows: int) -> List[List[int]]:
        result = [[1 for i in range(j+1)] for j in range(numRows)]
        if numRows > 2:
            for i in range(2, numRows):
                for j in range(1, len(result[i])-1):
                    result[i][j] = result[i-1][j-1] + result[i-1][j]
        return result
最后修改日期: 2021年1月8日

留言

撰写回覆或留言

发布留言必须填写的电子邮件地址不会公开。