1. 程式人生 > 其它 >leetcode 119 楊輝三角,一個數組解決

leetcode 119 楊輝三角,一個數組解決

技術標籤:leetcode

楊輝三角用多個數組很好解決,如果只用一個數組的話,涉及到迭代問題,以及不對稱,然而這些問題可以通過簡單地對齊和從後向前更新解決,程式碼如下:

class Solution:
    def getRow(self, rowIndex: int) -> List[int]:
        ret = [1]

        for i in range(rowIndex):
            ret.append(1)
            for j in range(i, 0, -1):
                ret[j] += ret[
j-1] return ret