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

问题描述:

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]
...
    原文作者:杨辉三角问题
    原文地址: https://blog.csdn.net/ysk0825/article/details/53825408
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞