LeetCode119

LeetCode119:https://leetcode-cn.com/problems/pascals-triangle-ii/submissions/

解题思路:在原来LeetCode118的基础上稍微修改了一下。

 1 class Solution:
 2     def getRow(self, rowIndex: int) -> List[int]:
 3         result = []#定义一个空数组,用于保存基本最终结果(二维数组))
 4         for i in range(rowIndex+1):#修改
 5             if i == 0:#第一行:[1]
 6                 result.append([1])
 7             elif i == 1:#第二行:[1,1]
 8                 result.append([1,1])
 9             else:# i >= 2#第三行以后
10                 prerow = result[-1]#上一行
11                 currow = []#当前行
12                 for j in range(i+1):#循环j,表示二维数组中的:第i行、第j列
13                     if j == 0 or j == i:#第一列和最后一列是1
14                         currow.append(1)
15                     else:#中间列,上一行的第j-1列元素 + 上一行的第j列元素
16                         currow.append(prerow[j-1] + prerow[j])
17                 result.append(currow)#将当前行添加到结果数组中
18         return result[-1]#返回结果数组 #修改返回结果
原文地址:https://www.cnblogs.com/vvzhang/p/14417264.html