Pascal's Triangle(杨辉三角)
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]
]
思路:动态规划,前面的构建出来了,后面根据某种关系也就可以构建出来。
这个要根据前一行来构建下一行。这里的for循环是直接遍历当前行的个数,往里面添加多少个。这里主要运用当前行的规律
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> result=new ArrayList<List<Integer>>();
if(numRows==0) return result;
List<Integer> row,pre=null;//row代表当前行,pre代表前一行
for(int i=1;i<=numRows;i++){//第几行,第几行就有几个元素
row=new ArrayList<>();
/*这里for循环中的步骤很巧妙,遍历当前行,若为开头或结尾,加1,其他情况再从前一行中计算,这样一开始就不会从前一行计算,所以不会出问题。
*/
for(int j=0;j<i;j++){//当前行的元素下标
if(j==0||j==i-1)
row.add(1);
else
row.add(pre.get(j-1)+pre.get(j));//一开始第一行第二行这里不会执行,
}
pre=row;
result.add(row);
}
return result;
}
还有一种更巧妙的,直接在一个list上操作。见代码。注意最后添加的时候new一个list,不然只能添加最终结果
class Solution {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> res=new ArrayList<List<Integer>>();
if(numRows==0) return res;
List<Integer> list=new LinkedList<>();
for(int i=1;i<=numRows;i++){
list.add(1);
for(int j=i-2;j>=1;j--){
list.set(j,list.get(j)+list.get(j-1));
}
//这里要新new一个list加进去,不然如果直接添加list,出现的情况是添加了最终结果(第一层for循环结束时的list)。
res.add(new LinkedList<Integer>(list));
}
return res;
}
}
Pascal's Triangle(杨辉三角)的更多相关文章
- [LeetCode] Pascal's Triangle 杨辉三角
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- [leetcode-118]Pascal's triangle 杨辉三角
Pascal's triangle (1过) Given numRows, generate the first numRows of Pascal's triangle. For example, ...
- LeetCode118. Pascal's Triangle 杨辉三角
题目 给定行数,生成对应的杨辉三角 思考 同一行是对称的,最大的下标为(行数+1)/2;1,1,2,3,6;下标从0开始,则对应分别为0.0.1.1.2.2 对于第偶数行,个数也是偶数,对于奇数行,个 ...
- 【LeetCode每天一题】Pascal's Triangle(杨辉三角)
Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. In Pascal's t ...
- [LeetCode] 119. Pascal's Triangle II 杨辉三角 II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...
- 20190105-打印字母C,H,N,口等图像和杨辉三角
1. 打印字母C ****** * * * * ****** def print_c(n): print('*' * n) for i in range(n): print('* ') print(' ...
- [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 118. Pascal's Triangle (杨辉三角)
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- [Swift]LeetCode118. 杨辉三角 | Pascal's Triangle
Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. In Pascal's t ...
随机推荐
- 4. React 属性和状态介绍
React 中的属性和状态初看之下可以互相替代,但是在 React 的设计哲学中两者有着截然不同的使用方式和使用场景. 属性的含义和用法 props = propert ...
- Android实现系统ROOT, 并能赋予app root权限
1. 获取root权限 --> 修改adb源码 a. 打开 system/core/adb/adb_main.cpp,或者是 system/core/adb/daemon/main.c ...
- 开源项目——小Q聊天机器人V1.4
小Q聊天机器人V1.0 http://blog.csdn.net/baiyuliang2013/article/details/51386281 小Q聊天机器人V1.1 http://blog.csd ...
- GDAL1.11版本对SHP文件索引加速测试
GDAL库中对于矢量数据的读取中可以设置一些过滤器来对矢量图形进行筛选,对于Shapefile格式来说,如果数据量太大,设置这个过滤器时间慢的简直无法忍受.好在GDAL1.10版本开始支持读取Shap ...
- 【一天一道LeetCode】#292. Nim Game
一天一道LeetCode 从今天开始,调整规律,不按顺序做,从easy开始! 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 ...
- 使用JavaScript动态的添加组件
使用JavaScript进行动态的网页窗体组件的添加是一件很方便也很容易实现的事情.话不多说,边看代码边做解释吧. 准备工作 由于html页面中不可以添加java代码,所以我在jsp页面中进行了测试. ...
- java7 新特性 总结版
Java7语法新特性: 前言,这是大部分的特性,但还有一些没有写进去,比如多核 并行计算的支持加强 fork join 框架:这方面并没有真正写过和了解.也就不写进来了. 1. switch中增加对S ...
- iOS中 超简单抽屉效果(MMDrawerController)的实现
ios开发中,展示类应用通常要用到抽屉效果,由于项目需要,本人找到一个demo,缩减掉一些不常用的功能,整理出一个较短的实例. 首先需要给工程添加第三方类库 MMDrawerController: 这 ...
- HTML5 window/iframe跨域传递消息 API
原文地址:HTML5′s window.postMessage API 在线示例:Using HTML5's window.postMessage(请打开控制台看日志) 原文日期: 2010年09月0 ...
- 通用数据水平层级选择控件v0.70升级版使其支持jQuery v1.9.1
升级原因:作者原来脚本支持的jquery版本太低了,查找了下资料,使得它能支持最新版本的jquery 备注说明:脚本代码源作者跟源文出处很难找,只能在此特感谢他的分享. 更新部分: 1.新版本不再支持 ...