1. 程式人生 > 實用技巧 >119.楊輝三角II

119.楊輝三角II

這道題和第118題是一樣的,需要注意這道題目對行數的要求

#定義一個列表,用來存放資料 num_list=[] forindex1inrange(rowIndex+1): #每一行要先新增一個空列表 num_list.append([]) #注意這裡的for迴圈的範圍 forindex2inrange(index1+1): #將值為一的位置規定好 ifindex1==0orindex2==0orindex2==index1: num_list[index1].append(1) #按照題目要求計算就好了 else: num_list[index1].append(num_list[index1-1][index2-1]+num_list[index1-1][index2]) returnnum_list[rowIndex]