""" 杨辉三角 II """ from typing import List class Solution: def getRow(self, rowIndex: int) -> List[int]: """ 索引从index=0开始 :param rowIndex: :return: """ if rowIndex == 0: return [1] result = [[1]] # range(1,1)不走遍历,所以要加1 for i in range(1, rowIndex + 1): row = [1] * (i + 1) for j in range(1, i): row[j] = result[i - 1][j - 1] + result[i - 1][j] # 外层等于rowIndex即可结束遍历 if i == rowIndex: return row result.append(row) if __name__ == '__main__': root = Solution().getRow(4) print(root)