输入: 5
输出:
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]
func generate(numRows int) [][]int {
res := make([][]int, numRows, numRows)
if numRows == 0 {
return res
}
res[0] = []int{1}
if numRows == 1 {
return res
}
for i := 1; i < numRows; i++ {
row := make([]int, i + 1)
row[0] = 1
for j := 0; j <= i; j++ {
if j == 0 || j == i {
row[j] = 1
} else {
row[j] = res[i - 1][j - 1] + res[i - 1][j]
}
}
res[i] = row
}
return res
}
// 执行用时 0ms
// 内存消耗 2MB