Force link :https://leetcode-cn.com/probl...
Their thinking :
Yang hui triangle
This question is actually a very meaningful one for me , Because all the algorithms come to the end , It may be that the abstract mathematical model is translated by using a certain computer language as a carrier , There is no difference between doing algorithm problems and doing math problems when reading . But who makes me bad at math ...
The specific analysis of the idea is as follows :
- Yang Hui triangle here can be considered as a two-dimensional array , Lines indicate the number of lines entered , Columns are the elements generated by each row , What you can know is that the number of lines is related to input , Each column contains elements equal to the number of columns it is in ( from 1 start )
- In the number of each column , The first and last numbers are 1( The first column can be regarded as the first , And the last ), The rest of the figures fi = fi-1 + fi-1
- With the above analysis , Then the solution is ready
func yanghuiTriangle(numRows int) [][]int {
res := make([][]int, numRows)
for i := range res {
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
}