其他分享
首页 > 其他分享> > 118. 杨辉三角、Leetcode的Go实现

118. 杨辉三角、Leetcode的Go实现

作者:互联网

118. 杨辉三角

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

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

 示例 1:

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

输入: numRows = 1
输出: [[1]]
 

提示:

1 <= numRows <= 30

模拟:

func generate(numRows int) [][]int {
    // 数字模拟过程
    res:=make([][]int,numRows)
    for i:=0;i<numRows;i++{
        // 边上为1的先赋值
        res[i]=make([]int,i+1)
        res[i][0]=1
        res[i][i]=1
        for j:=1;j<i;j++{
            res[i][j]=res[i-1][j]+res[i-1][j-1]
        }
    }
    return res
}

标签:示例,int,numRows,Go,杨辉三角,118,输入
来源: https://blog.csdn.net/qq_42410605/article/details/122410627