LeetCode 118. Pascal's Triangle
程序员文章站
2022-04-01 11:31:05
...
题目
思路
用一个list记录上一层的数字,用来更新当前层。
代码
class Solution:
def generate(self, numRows):
"""
:type numRows: int
:rtype: List[List[int]]
"""
res_list = []
last_list = []
for i in range(1, numRows + 1):
tmp_list = []
for j in range(i):
if j == 0: tmp_list.append(1)
elif j == i - 1: tmp_list.append(1)
else: tmp_list.append(last_list[j - 1] + last_list[j])
last_list[:] = tmp_list[:]
res_list.append(tmp_list[:])
return res_list
上一篇: 绘制三角形
推荐阅读
-
Leetcode No.119 Pascal's Triangle II(c++实现)
-
杨辉三角(pascal's triangle)
-
LeetCode-119. Pascal's Triangle II
-
119. Pascal's Triangle II [杨辉三角形2]
-
119. Pascal's Triangle II
-
leetcode -- 119. Pascal's Triangle II
-
2018.05.03 leetcode #119. Pascal's Triangle II
-
【leetcode】119. Pascal's Triangle II
-
LeetCode 119. Pascal's Triangle II
-
[LeetCode]119. Pascal's Triangle II