Force link :https://leetcode-cn.com/probl...
Their thinking :
- Solution 1 : Use the solution of Yang Hui triangle , Returns a one bit array of fixed subscripts , This solution requires a two-dimensional array
func getRow(rowIndex int) []int {
res := make([][]int, rowIndex + 1)
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-1] + res[i-1][j]
}
}
return res[rowIndex]
}
2. Optimize one : Actually, we are calculating row+1 Line time , Only the second row Row data , So it can be used Scrolling array To optimize the space
func getRow(rowIndex int) []int {
pre, cur := []int, []int
for i := range rowIndex {
cur[i] = make([]int, i + 1)
cur[i][0] = 1
cur[i][i] = 1
for j := 1; j < i; j++ {
cur[j] = pre[j-1] + pre[j]
}
pre = cur
}
return pre
}