题目:
给定一个非负整数 numRows,生成杨辉三角的前 numRows 行。
在杨辉三角中,每个数是它左上方和右上方的数的和。
示例:
输入: 5 输出: [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ]
来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/pascals-triangle/
解法一:
class Solution:
def generate(self, numRows: int) -> List[List[int]]:
lists = []
for i in range(numRows):
x = 0
lists.append([0]*(i+1))
if i == 0:
lists[0][0] = 1
continue
if (i+1)%2==0:
w = (i+1)//2
else:
w = (i+1)//2+1
for j in range(w):
lists[i][j] = x + lists[i-1][j]
lists[i][i-j] = lists[i][j]
x = lists[i-1][j]
return lists
来源:CSDN
作者:xin3475
链接:https://blog.csdn.net/xin3475/article/details/104040322