杨辉三角
- 难度: 简单
- 通过率: 43.5%
- 题目链接:https://leetcode-cn.com/problems/pascals-triangle
题目描述
给定一个非负整数 numRows,生成杨辉三角的前 numRows 行。
在杨辉三角中,每个数是它左上方和右上方的数的和。
示例:
输入: 5 输出: [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ]
解法:
仿佛回到了大学一年级。
class Solution:
def generate(self, n_rows: int) -> List[List[int]]:
triangle = []
last_row = []
for i_row in range(n_rows):
row_len = i_row + 1
row = [1] * row_len
for i in range(1, row_len - 1):
row[i] = last_row[i-1] + last_row[i]
triangle.append(row)
last_row = row
return triangle