楊輝三角 II

class Solution:
    
    def getNSample(self, n) -> List[int]:
        
        result = []
        sample = 1
        for i in range(1, n + 1):
            sample *= i
            result.append(sample)
        return result
    
    def getRow(self, rowIndex: int) -> List[int]:
        if rowIndex == 0:
            return [1]
        rowIndex += 1
        result = []
        nSample = self.getNSample(rowIndex - 1)
        result.append(1)
        for i in range(1, rowIndex - 1):
            result.append(int(nSample[-1] / (nSample[i - 1] * nSample[rowIndex - i - 2])))
        result.append(1)
        return result

這個版本不錯,速度飛快,主要是用了組合公式

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