leetcode-119. 杨辉三角 II 运行时间超越100%

一、题目要求

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

leetcode-119. 杨辉三角 II 运行时间超越100%

在杨辉三角中,每个数是它左上方和右上方的数的和。


二、代码及思路(这里思路可看我上一篇博文求整个杨辉三角形)

https://blog.****.net/GrinAndBearIt/article/details/80338648

class Solution(object):
    def getRow(self, rowIndex):
        """
        :type rowIndex: int
        :rtype: List[int]
        """
        result=[]
        for i in range(0,rowIndex+1):   #这里是返回第rowIndex行所以需要计算前rowIndex+1行的杨辉三角形
            result.append([1])
            for j in range(1,i):
                result[i].append(result[i-1][j-1]+result[i-1][j])
            if i>0:   
                result[i].append(1)

        return result[-1]

3、运行结果

leetcode-119. 杨辉三角 II 运行时间超越100%