代码拉取完成,页面将自动刷新
package code;
/*
* 118. Pascal's Triangle
* 题意:上层元素相邻的和,两边补1,得到下层元素
* 难度:Easy
* 分类:Array
* 思路:迭代计算
* Tips:
*/
import java.util.ArrayList;
import java.util.List;
public class lc118 {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> res = new ArrayList<>();
if(numRows==0) return res;
List<Integer> ls = new ArrayList();
ls.add(1); //第一层1个1
res.add(ls);
numRows--;
if(numRows==0) return res;
while(numRows>0){
ArrayList<Integer> temp = new ArrayList();
temp.add(1);
for (int i = 0; i < ls.size()-1 ; i++) {
temp.add(ls.get(i)+ls.get(i+1));
}
temp.add(1);
res.add(temp);
ls = temp;
numRows--;
}
return res;
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。