Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle.
Note that the row index starts from 0.

In Pascal's triangle, each number is the sum of the two numbers directly above it.
Example:
Input: 3
Output: [1,3,3,1]
Follow up:
Could you optimize your algorithm to use only O(k) extra space?
<思路>和帕斯卡三角形Ι一样。重点在于 level = [1] + [level[i]+level[i+1] for i in range(len(level)-1)] + [1]
等号左边的level是新的一层,右边的level指的是上一层。然后将上一层第0个数和第1个数相加,作为新一层的第1个数,依次循环。(第0个和最后一个永远都是[1])
class Solution(object):
def getRow(self, rowIndex):
"""
:type rowIndex: int
:rtype: List[int]
"""
level = [1]
while rowIndex>0:
level = [1] + [level[i]+level[i+1] for i in range(len(level)-1)] + [1]
rowIndex -= 1
return level
帕斯卡三角形第K行算法

295

被折叠的 条评论
为什么被折叠?



