首页 > 其他分享 >杨辉三角 II

杨辉三角 II

时间:2023-02-16 14:35:22浏览次数:42  
标签:numRows return rowIndex res II 杨辉三角 row

给定一个非负整数 numRows生成「杨辉三角」的前 numRows 行。

在「杨辉三角」中,每个数是它左上方和右上方的数的和。

示例 1:

输入: numRows = 5
输出: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]

示例 2:
输入: numRows = 1
输出: [[1]]

# 解题:

class Solution(object):
    def getRow(self, rowIndex):
        """
        :type rowIndex: int
        :rtype: List[int]
        """
        if rowIndex == 1:
            return [1,1]
        if rowIndex == 0:
            return [1]
        rowIndex +=1
        res = [[1] * (i + 1) for i in range(rowIndex)]
        row = []
        if rowIndex > 2:
            for i in range(2, rowIndex):
                for j in range(1, i):
                    res[i][j] = res[i - 1][j - 1] + res[i - 1][j]
                row = res[rowIndex-1]
        return row

 

标签:numRows,return,rowIndex,res,II,杨辉三角,row
From: https://www.cnblogs.com/jizg/p/17126642.html

相关文章