Given a non-negative integer numRows, generate the first numRows of Pascal’s triangle.
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
留言