给定 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;
}
}

最新文章

  1. show master/slave status求根溯源
  2. POJ 3080 Blue Jeans (多个字符串的最长公共序列,暴力比较)
  3. EasyUI datagrid添加右键菜单项
  4. Android自定义控件步骤总结
  5. nefu 449 超级楼梯 &amp;&amp;nefu 911 跨楼梯
  6. 将一个字典内的内value转换为集合:返回一个数组,此数组中包含输入字典的键值对中的数组的所有元素(为NSArray添加category)
  7. echart 饼状图自定义样式
  8. O(N) 求数组中最大子串和
  9. vue--传值
  10. GC ROOT
  11. sonar——Synchronized classes Vector, Hashtable, Stack and StringBuffer should not be used
  12. 如何通过ajax来获取返回值
  13. react-create-app
  14. @ContextConfiguration的意思
  15. Java基础-内部类介绍
  16. 2017-2018-1 20155305 《信息安全系统设计基础》第四周学习总结(课堂提交作业未来得及提交码云链接myod补充博客)
  17. Oracle linux安装Oracle 11G
  18. AngularJS学习笔记(2)——与用户交互的动态清单列表
  19. 为什么 .NET 会被叫做 .NET?
  20. maven modules

热门文章

  1. mysql general log开启
  2. js正则表达式,密码长度要大于6位,由数字和字母组成
  3. hdu-4857 逃生(拓扑序)
  4. Spring 事务管理高级应用难点剖析: 第 1 部分
  5. C++之函数适配器--绑定器bind原理图解
  6. Win10+CUDA9.0+cudnn7.1安装
  7. C - Present
  8. 2.9-2.10 hive中常见查询
  9. php防盗链技术
  10. 【剑指Offer学习】【面试题66:矩阵中的路径】