Resolve Pascal’s Triangle in Python

0
107

[ad_1]

The problem

Given an integer numRows, return the primary numRows of Pascal’s triangle.

In Pascal’s triangle, every quantity is the sum of the 2 numbers straight above it as proven:

Instance 1:

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

Instance 2:

Enter: numRows = 1
Output: [[1]]

Constraints:

The answer in Python code

class Answer: def generate(self, numRows: int) -> Listing[List[int]]: triangle = [[1]] for j in vary(1, numRows): prev = triangle[-1] triangle.append([1] + [prev[i]+prev[i+1] for i in vary(len(prev)-1)] + [1]) return triangle

Code language: Python (python)

[ad_2]