from typing import List
class Solution:
def generate(self, numRows: int) -> List[List[int]]:
# 定义一个列表,用来存放数据
num_list = []
for index1 in range(numRows):
# 每一行要先添加一个空列表
num_list.append([])
# 注意这里的for循环的范围
for index2 in range(index1 + 1):
# 将值为一的位置规定好
if index1 == 0 or index2 == 0 or index2 == index1 :
num_list[index1].append(1)
# 按照题目要求计算就好了
else:
num_list[index1].append(num_list[index1 - 1][index2 - 1] + num_list[index1 - 1][index2])
return num_list

A = Solution()
print(A.generate(5))
扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄