Given a non-negative integer numRows, generate the former numRows of the Yanghui triangle.

In Yang Hui’s triangle, each number is the sum of the numbers on its upper left and upper right.

Example:

Input: 5 output: [[1], [1, 1], [1, 2, 1],,3,3,1 [1], [1,4,6,4,1]]

Answer:

class Solution:
    def generate(self, numRows: int) - >List[List[int]] :
        result = [ [1] * (i+1) for i in range(numRows)]
        if numRows>=3:
            for i in range(2,numRows):
                for j in range(1,i):
                    result[i][j] = result[i-1][j-1] + result[i-1][j]
        return result
Copy the code