当前位置:网站首页>Leetcode topic [array] -118 Yang Hui triangle

Leetcode topic [array] -118 Yang Hui triangle

2022-07-06 21:36:00 LabRat

Force link :https://leetcode-cn.com/probl...
Their thinking :
image.png

                                     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 :

  1. 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 )
  2. 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
  3. 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
}
原网站

版权声明
本文为[LabRat]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202131118365537.html