1、双循环
class Solution:
def generate(self, numRows: int) -> List[List[int]]:
if numRows==1:
return [[1]]
# 一二层
res = [[1],[1,1]]
# 之后的前后各增加1以及上一层相近的两个数之和:res[i-1][j]+res[i-1][j+1]
for i in range (2,numRows):
res.append([1])
for j in range(i-1):
res[i].append(res[i-1][j]+res[i-1][j+1])
res[i].append(1)
return res
2、使用递归(待更)
119题:
class Solution:
def getRow(self, rowIndex: int) -> List[int]:
num = list()
for i in range(0,rowIndex+1):
num.append(1)
# 从后往前加并修改数值
for j in range(i-1,0,-1):
num[j] += num[j-1]
return num