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))
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/103786.html
標籤:Python
上一篇:Python--發送郵件
下一篇:119.楊輝三角II
