题目描述:
思路:
- 当前行头元素先追加 1
- 当前行除了第一和最后一个元素的值(i,j)=上一行(i-1,j-1)+上一行(i-1,j)
- 遍历完,当前行追加1
class Solution:
def generate(self, numRows: int) -> List[List[int]]:
res=[]
while numRows:
temp=[1]
if not res:
res.append(temp)
else:
n=len(res[-1])
for i in range(n-1):
temp.append(res[-1][i]+res[-1][i+1])
# temp.append(su)
temp.append(1)
res.append(temp)
numRows-=1
return res
来源:https://blog.csdn.net/weixin_38664232/article/details/100031106