力扣118.楊輝三角
給定一個非負整數 numRows,生成楊輝三角的前 numRows 行,

在楊輝三角中,每個數是它左上方和右上方的數的和,
示例:
輸入: 5
輸出:
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]
題解:
class Solution:
def generate(self, n: int) -> List[List[int]]:
return [[comb(i,j) for j in range(i+1)] for i in range(n)]
emm,,,有點水題了,
func generate(n int) [][]int {
var res [][]int
for i := 0; i < n; i++ {
v := make([]int, i+1)
for j := range v {
v[j] = 1
}
res = append(res, v)
for j := 1; j < i; j++ {
res[i][j] = res[i-1][j-1] + res[i-1][j]
}
}
return res
}

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/231009.html
標籤:python
上一篇:手寫演算法-python代碼實作Ridge(L2正則項)回歸
下一篇:python爬取有道詞典
