LeetCode(30)-Pascal's Triangle
2016-04-07 12:38
393 查看
题目:
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] ]
思路
题意是要用数组表示帕斯卡三角形输入一个数值,显示相应行数的【帕斯卡三角形
根据下一行和上一行的递推公式来处理
设置一个变量fisrt来记录上一行的数据,最新一行的是新建的
-
代码
public class Solution { public List<List<Integer>> generate(int numRows) { List<List<Integer>> all = new ArrayList<List<Integer>>(); List<Integer> first = new ArrayList<Integer>(); if(numRows == 0){ return all; } if(numRows == 1){ List<Integer> a1 = new ArrayList<Integer>(); a1.add(1); all.add(a1); return all; } if(numRows > 1){ List<Integer> a2 = new ArrayList<Integer>(); a2.add(1); all.add(a2); } for(int i = 1;i < numRows;i++){ List<Integer> next = new ArrayList<Integer>(); next.clear(); next.add(1); for(int a = 1;a < i;a++){ if(a-1 >= 0 ) next.add(first.get(a-1)+first.get(a)); } next.add(1); all.add(next); first.clear(); if(next != null){ for(int o = 0;o < next.size();o++){ first.add(next.get(o)); } } } return all; } }
相关文章推荐
- Unity3d平台预编译
- jvm的内存空间分块情况
- 网路:JSON 解析(天气预报)
- 逆向工程实战--Acid burn
- Ubuntu Server上的LVM配置
- POJ 1730 Perfect Pth Powers
- RSA算法原理(二)
- RSA算法原理(一)
- Windows下安装以及配置nginx
- JavaScript 的14道题
- 网页屏蔽Backspace事件,输入框不屏蔽
- HDU 1686
- 关于Delphi字符串内存泄露的问题
- InvalidDataAccessApiUsageException 和 Write operations are not allowed in read-only mode 解决方法
- Oracle事务概念
- scp -r authorized_keys zdb14:~/.ssh/ root@zdb14's password: bash: scp: command not found lost conn
- SPOJ DIVSUM Divisor Summation
- plsql访问oracle数据库中文乱码问题解决方法
- python list列表sort、sorted、reverse排序
- Java回调机制解析