problem_solving_09.py 722 字节
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
"""
杨辉三角 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)