118 Pascal's Triangle 帕斯卡三角形 杨辉三角形
2024-10-15 11:48:58
给定 numRows, 生成帕斯卡三角形的前 numRows 行。
例如, 给定 numRows = 5,
返回
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]
详见:https://leetcode.com/problems/pascals-triangle/description/
Java实现:
class Solution {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> res=new ArrayList<List<Integer>>();
if(numRows==0){
return res;
}
List<Integer> row = new ArrayList<>();
//ArrayList中的set(index, object)和add(index, object)的区别:set:将原来index位置上的object的替换掉;add:将原来index位置上的object向后移动
for (int i = 0; i < numRows; i ++) {
row.add(0, 1);
for (int j = 1; j < row.size() - 1; j ++) {
row.set(j, row.get(j) + row.get(j + 1));
}
res.add(new ArrayList<>(row));
}
return res;
}
}
最新文章
- show master/slave status求根溯源
- POJ 3080 Blue Jeans (多个字符串的最长公共序列,暴力比较)
- EasyUI datagrid添加右键菜单项
- Android自定义控件步骤总结
- nefu 449 超级楼梯 &;&;nefu 911 跨楼梯
- 将一个字典内的内value转换为集合:返回一个数组,此数组中包含输入字典的键值对中的数组的所有元素(为NSArray添加category)
- echart 饼状图自定义样式
- O(N) 求数组中最大子串和
- vue--传值
- GC ROOT
- sonar——Synchronized classes Vector, Hashtable, Stack and StringBuffer should not be used
- 如何通过ajax来获取返回值
- react-create-app
- @ContextConfiguration的意思
- Java基础-内部类介绍
- 2017-2018-1 20155305 《信息安全系统设计基础》第四周学习总结(课堂提交作业未来得及提交码云链接myod补充博客)
- Oracle linux安装Oracle 11G
- AngularJS学习笔记(2)——与用户交互的动态清单列表
- 为什么 .NET 会被叫做 .NET?
- maven modules