[Leetcode] 119. 杨辉三角 II Python3

 

 给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行。

 

和118题类似,看不懂可以看一下我118题的博文。https://blog.csdn.net/nicehou666/article/details/81353033

题中的第K行指的是不算[1] ,而是从[1,1]开始,即[1,1]是第一行。也就是说,输入是3,但返回的是第4行。

所以在for循环时,rowIndex需要加1。

class Solution:
    def getRow(self, rowIndex):
        """
        :type rowIndex: int
        :rtype: List[int]
        """
        r = [[1]]
        for i in range(1,rowIndex+1):
            r.append(list(map(lambda x,y:x+y, [0]+r[-1],r[-1]+[0])))
        return r[rowIndex]

猜你喜欢

转载自blog.csdn.net/niceHou666/article/details/81354808