给定一个非负整数 numRows,生成杨辉三角的前 numRows 行。
在杨辉三角中,每个数是它左上方和右上方的数的和。
示例:
输入: 5
输出:
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]
class Solution:
def generate(self, numRows: int) -> List[List[int]]:
result = []
if numRows == 0:
return result
else:
old = [1]
new = []
result.append(old)
for i in range(2, numRows+1):
for j in range(i):
if j == 0:
new.append(old[j])
elif j == i-1:
new.append(old[j-1])
else:
new.append(old[j-1]+old[j])
result.append(new)
old = new
new = []
return result
LeetCode-118 杨辉三角
猜你喜欢
转载自blog.csdn.net/qq_24502469/article/details/104130473
今日推荐
周排行