這道題和第118題是一樣的,需要注意這道題目對行數的要求
# 定義一個串列,用來存放資料 num_list = [] for index1 in range(rowIndex + 1): # 每一行要先添加一個空串列 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[rowIndex]轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/103787.html
標籤:Python
上一篇:118.楊輝三角
下一篇:shelve模塊讀寫檔案報錯
