# [leetcode in golang]119、杨辉三角 II

aside section ._1OhGeD · · 653 次点击 · · 开始浏览

[1,3,3,1]

[(0)+1,1+3,3+3,3+1,1]
[1,4,6,4,1]

``````func getRow(rowIndex int) []int {
res:=make([]int,rowIndex+1)
for t:=range res{
res[t]=0
}
res[0]=1
for i:=1;i<rowIndex+1;i++{
for j:=i;j>=1;j--{
res[j]=res[j-1]+res[j]
}
}
return res
}

``````

#### 数组初始化遇到问题：non-constant array bound:

You can't instantiate an array like that with a value calculated at runtime. Instead use make to initialize a slice with the desired length. It would look like this;

``````left := make([]int, leftLength)
``````

0 回复

• 请尽量让自己的回复能够对别人有帮助
• 支持 Markdown 格式, **粗体**、~~删除线~~、``单行代码``
• 支持 @ 本站用户；支持表情（输入 : 提示），见 Emoji cheat sheet
• 图片支持拖拽、截图粘贴等方式上传

[1,3,3,1]

[(0)+1,1+3,3+3,3+1,1]
[1,4,6,4,1]

``````func getRow(rowIndex int) []int {
res:=make([]int,rowIndex+1)
for t:=range res{
res[t]=0
}
res[0]=1
for i:=1;i<rowIndex+1;i++{
for j:=i;j>=1;j--{
res[j]=res[j-1]+res[j]
}
}
return res
}

``````

#### 数组初始化遇到问题：non-constant array bound:

You can't instantiate an array like that with a value calculated at runtime. Instead use make to initialize a slice with the desired length. It would look like this;

``````left := make([]int, leftLength)
``````