Pascal's Triangle 解答
Question
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]
]
Solution
Key to the solution is to use two arrays, one for current list, one for previous array.
 public class Solution {
     public List<List<Integer>> generate(int numRows) {
         List<List<Integer>> result = new ArrayList<List<Integer>>();
         if (numRows == 0)
             return result;
         List<Integer> prev = new ArrayList<Integer>();
         prev.add(1);
         result.add(prev);
         while (numRows > 1) {
             List<Integer> current = new ArrayList<Integer>();
             int length = prev.size();
             current.add(1);
             for (int i = 0; i < length - 1; i++)
                 current.add(prev.get(i) + prev.get(i + 1));
             current.add(1);
             result.add(current);
             prev = current;
             numRows--;
         }
         return result;
     }
 }
Pascal's Triangle 解答的更多相关文章
- Pascal's Triangle II 解答
		
Question Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Ret ...
 - [LeetCode] Pascal's Triangle II 杨辉三角之二
		
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...
 - [LeetCode] Pascal's Triangle 杨辉三角
		
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
 - 【leetcode】Pascal's Triangle II
		
题目简述: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Retur ...
 - 【leetcode】Pascal's Triangle
		
题目简述: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5 ...
 - LeetCode 118 Pascal's Triangle
		
Problem: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows ...
 - LeetCode 119 Pascal's Triangle II
		
Problem: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Ret ...
 - LeetCode - Pascal's Triangle II
		
题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return ...
 - 【leetcode】Pascal's Triangle I & II (middle)
		
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
 
随机推荐
- UVa10815.Andy's First Dictionary
			
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
 - vim 的配色方案
			
浅色: http://www.vimninjas.com/2012/09/14/10-light-colors/ 深色: http://www.vimninjas.com/2012/08/26/10- ...
 - Python 协程(gevent)
			
协程,又叫微线程,协程是一种用户态的轻量级线程. 协程拥有自己的寄存器上下文和栈.协程调度切换时,将寄存器上下文和栈保存到其他地方,在切回来的时候,恢复先前保存的寄存器上下文和栈.因此: 协程能保留上 ...
 - 基于google  earth engine 云计算平台的全国水体变化研究
			
第一个博客密码忘记了,今天才来开通第二个博客,时间已经过去两年了,三年的硕士生涯,真的是感慨良多,最有收获的一段时光,莫过于在实验室一个人敲着代码了,研三来得到中科院深圳先进院,在这里开始了新的研究生 ...
 - 在Maven的配置文件中,自定义私有仓库地址和设置下载的jar包的保存位置
			
在Maven的settings.xml,可以设置Maven的私有仓库的地址,还可以设置所下载jar包在自己电脑的保存地址(默认不设置保存在个人文件夹的.m2文件夹下). 1.设置私有仓库地址: < ...
 - Node.JS + MongoDB技术浅谈
			
看到一个Node.JS + MongoDB的小样例,分享给大家.魔乐科技软件学院(www.mldnjava.cn)的讲座 Node.JS + MongoDB技术讲座 云计算 +大数据 ...
 - 大到可以小说的Y组合子(三)
			
答:关于Fix的问题你fix了吗? 问:慢着,让我想想,上次留下个什么问题来着?是说我们有了一个求不动点的函数Fix,但Fix却是显式递归的,是吧? 答:有劳你还记的这个问题. 问:Fix的参与背离了 ...
 - 解决ActiveX Control异常:"没有注册类(异常来自 HRESULT:0x80040154(REGDB_E_CLASSNOTREG))"
			
问题背景: 1.我们的程序是用winform调用unity web player 插件来作为播放器在客户端播放动画文件的. 2.播放器是由我们的客户端程序调用的 3.客户端程序默认是以管理员身份启动的 ...
 - android——屏幕适配大全(转载)
			
http://my.oschina.net/u/2008084/blog/496161 一.适配可行性 早在Android设计之初就考虑到了这一点,为了让app适应标准or山寨屏幕,google已经有 ...
 - J2EE 中    The function valueOf must be used with a prefix when a default namespace is not specified 错误
			
jsp页面中,JSTL El表达式字符串比较常用方法 fn:contains 判断字符串是否包含另外一个字符串 <c:if test="${fn:contains(name, sear ...