您的位置:首页 > 编程语言 > Python开发

[leetcode, python] Pascal's Triangle II 杨辉三角

2016-12-23 00:27 204 查看
问题描述:

Given an index k, return the kth row of the Pascal's triangle.

For example, given k = 3,
Return [1,3,3,1].


解决方案:

class Solution(object):
def getRow(self, rowIndex):
"""
:type rowIndex: int
:rtype: List[int]
"""
result = [1]
for num in range(rowIndex):
result = [sum(i) for i in zip([0] + result, result + [0])]
return result


思路说明:

下一行的结果 = 上一行复制两份,错位相加(空位补0)。如:
[1,1] = [0,1] + [1,0]
[1,2,1] = [0,1,1] + [1,1,0]
[1,3,3,1] = [0,1,2,1] + [1,2,1,0]
...
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息