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 ...
随机推荐
- OpenWrt opkg 在线源默认配置
dest root /dest ram /tmplists_dir ext /var/opkg-listsoption overlay_root /overlaysrc/gz barrier_brea ...
- ./scripts/feeds update -a OpenWrt大招系列
./scripts/feeds update -a Updating feed 'packages' from 'https://github.com/openwrt/packages.git' .. ...
- OpenStack Mixture HypervisorsDriver configure and implementation theory
通过本文,您将可以了解在 OpenStack 中如何进行混合 Hypervisor 的配置及其实现原理的基本分析.本文主要结合作者在 Nova 中的实际开发经验对 OpenStack 中混合 Hype ...
- iPhone/iPad全屏截图与区域截图的几种方法
本文由社区会员taonavy分享 截取本区域(self.view): UIGraphicsBeginImageContext(CGSizeMake(self.view.frame.size.width ...
- Unity3D 灰度shader(改编自NGUI)
原创文章如需转载请注明:转载自 脱莫柔Unity3D学习之旅 QQ群:[] 本文链接地址: 灰度shader 废话不多说,直接图解流程: 1.原图 2.改动shader 打开NGUI自带的shader ...
- Oracle varchar 字段排序问题
数据库字段: 想要的结果: 实际查询的结果: 解决方法:使用CAST函数把varchar2转为int类型 order by CAST(CODE AS INTEGER)
- IOS中的各种Picker
简述 在应用的一些设置中经常要用到一些Picker来快速帮助用户选定取值,一般会用到的有UIDatePicker,UIPickerView以及UIImagePickerController. 初始界面 ...
- iOS开发CoreAnimation解读之一——初识CoreAnimation核心动画编程
iOS开发CoreAnimation解读之一——初识CoreAnimation核心动画编程 一.引言 二.初识CoreAnimation 三.锚点对几何属性的影响 四.Layer与View之间的关系 ...
- 利用PHP/MYSQL实现的简易微型博客(转)
数据库:ly_php_base 表:ly_micro_blog(仅仅有一个表)字段:id,title,date,content,hits 文件: 文件 描述 default.php 默认主页.显示博文 ...
- JavaScript自我学习之解析与执行
如果想要学好JavaScript那么我们首先必须要知道浏览器JavaScript引擎是如何解释执行JavaScript代码的,作为一名菜鸟,从自己学习JavaScript的过程来说,真心觉得不了解这些 ...