leetcode 刷題 119. 楊輝三角II

給定一個非負索引 k,其中 k ≤ 33,返回楊輝三角的第 k 行。

在楊輝三角中,每個數是它左上方和右上方的數的和。

示例:

輸入: 3
輸出: [1,3,3,1]

解答:

class Solution:
    def getRow(self, rowIndex: int) -> List[int]:
        rownum = rowIndex + 1
        result = [ [1] * (i+1) for i in range(rownum)]
        if rownum>=3:
            for i in range(2,rownum):
                for j in range(1,i):
                    result[i][j] = result[i-1][j-1] + result[i-1][j]
        output = result[rowIndex]
        return output

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章