Leetcode Pascal's Triangle
2015-10-06 02:47
381 查看
Given numRows, generate the first numRows of Pascal's triangle.
For example, given numRows = 5,
Return
Java code:
Reference:
1. http://www.programcreek.com/2014/03/leetcode-pascals-triangle-java/
For example, given numRows = 5,
Return
[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ]
解题思路:
用上一次生成的array 生成新的array.Java code:
public List<List<Integer>> generate(int numRows) { List<List<Integer>> result = new ArrayList<List<Integer>>(); for(int i = 0; i < numRows; i++) { List<Integer> arr = new ArrayList<Integer>(); if(result.size() == 0) { arr.add(1); }else { List<Integer> old = result.get(result.size()-1); arr.add(1); for(int j = 0; j < old.size()-1; j++) { arr.add(old.get(j) + old.get(j+1)); } arr.add(1); } result.add(arr); } return result; }
Reference:
1. http://www.programcreek.com/2014/03/leetcode-pascals-triangle-java/
相关文章推荐
- PHP判断是否为手机客户端
- PHP获取文件夹内所有文件包括子目录文件的名称或路径
- 口红效应
- ssh插入信息报异常!don't flush the Session after an exception occurs
- UVA 10288 Coupons
- 值得深思的连续赋值--赋值运算符运算顺序
- Delphi中增强程序的不同Windows版本的兼容性技巧
- 网页布局(div)
- 关于spring注入
- 注意Thread.interrupt()方法的真正作用并不是用来中断线程
- Leetcode Majority Element II
- Linux常见应用--DNS基本工作原理
- LeetCode #10 Regular Expression Matching (H)
- PHP面试体小总结
- 【优化】命令行模式的RSS阅读器
- Java 方法重写与重载的区别
- Kienct2.0+Opencv获取面部信息
- 利用Common-Lang为Java class自动提供toString()支持
- Linux中的查看已挂载的文件系统命令
- Delphi中的值传递与址传递