您的位置:首页 > 理论基础 > 数据结构算法

LeetCode 数据结构—杨辉三角

2021-09-25 14:55 591 查看

 

 我们可以知道前一排的数字可以直接影响到后一排的取值,且从第一排开始后,之后的首尾都是1.所以如果暴力的话,也可以直接得到正确结果。

public List<List<Integer>> generate(int numRows) {
List<List<Integer>> res=new LinkedList<List<Integer>>();
List<Integer> index=new LinkedList<>();
index.add(1);
res.add(index);
if(numRows==1)
return res;
for(int i=1;i<numRows;i++){
List<Integer> LastRow=res.get(i-1);
List<Integer> thisRow=new LinkedList<>();
thisRow.add(1);
for(int j=0;j<LastRow.size()-1;j++)
{
thisRow.add(LastRow.get(j)+LastRow.get(j+1));
}
thisRow.add(1);
res.add(thisRow);
}
return res;
}

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: