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

来源:互联网 发布:双代号网络图软件 编辑:程序博客网 时间:2024/05/29 17:02

问题描述:

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]...
0 0