Pascal's Triangle
2015-07-03 11:19
330 查看
题目:
Given numRows, generate the first numRows of Pascal's triangle.
For example, given numRows = 5,
Return
Given numRows, generate the first numRows of Pascal's triangle.
For example, given numRows = 5,
Return
[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] code:
public class Solution { public ArrayList<ArrayList<Integer>> generate(int numRows) { ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>(); if(numRows<=0) { return result; } ArrayList<Integer> pre = new ArrayList<Integer>(); pre.add(1); result.add(pre); for(int i=2; i<=numRows; i++) { ArrayList<Integer> cur = new ArrayList<Integer>(); cur.add(1); //first for (int j=0; j<pre.size()-1;j++) { cur.add(pre.get(j)+pre.get(j+1)); //middle } cur.add(1); //last result.add(cur); pre=cur; } return result; } }
相关文章推荐
- Yii rules常用规则 ,适用Yii2
- Otsu(最大类间方差法)方法的C++实现
- Linux基础命令使用
- Jsoup学习笔记2:Jsoup解析HTML代码标签与属性
- 定义游标
- 银行取款[多线程]{使用同步代码块确保线程同步}
- JS 设置属性值
- Linux如何获取命令使用帮助说明
- java实现定时任务的三种方法
- rsync+inotify实现同步数据
- 线程
- JVM的内存区域划分
- VS2008 error C2664:不能将参数 2 从“const char [5]”转换为“LPCTSTR”
- ORACLE 常见面试题
- iOS开发范例实战宝典(进阶篇)
- MyEclipse每次修改js有另一种部署
- 【RAC】 RAC For W2K8R2 安装--dbca创建数据库(七)
- ubuntu 常见命令
- xdebug安装教程
- ExtJs 入门教程十九[其它 :Ext]