157 字
1 分钟
LeetCode-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]]
思路(手撕)
与 70. 爬楼梯 类似
func generate(numRows int) [][]int { if numRows == 1 { return [][]int{{1}} }
if numRows == 2 { return [][]int{{1},{1, 1}} }
res := [][]int{{1}, {1, 1}} for i := 2; i < numRows; i++ { path := make([]int, i+1) path[0], path[len(path)-1] = 1, 1 for j := 1; j < i; j++ { path[j] = res[i-1][j-1] + res[i-1][j] } res = append(res, path) } return res} LeetCode-118. 杨辉三角
https://sheep44044.github.io/posts/算法/动态规划/leetcode-118-杨辉三角/