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 ...
随机推荐
- 07 总结ProgressDialog 异步任务
1,ProgressDialog > //使用对象 设置标题 progressDialog.setTitle("标题"); ...
- 步步为营---- MuleEsb学习(一) 扫盲篇
本篇文章是基于不断的接触GXPT之后,对其技术开始不断的积累学习^^^,有很多问题带给我了思考,对于如何的处理各个部分的流程?这个如何处理?太多的问题促使着我一步一步的学习,在师哥们的指导下,逐步的清 ...
- Windows自删除程序和DLL
Windows自删除程序和DLL 参照文章 http://blog.csdn.net/rxxi/article/details/741557 做了个自删除的程序SelfDelete.代码下载(我的FT ...
- 定制Maven原型生成项目
1自定义原型 1.1创建原型项目 要定制自己的原型,首先就要创建原型项目来进行定制: mvnarchetype:create -DgroupId=com.cdai.arche -DartifactId ...
- boost::bad_weak_ptr的原因
出现boost::bad_weak_ptr最可能的原因是enable_shared_from_this<>类构造函数中调用shared_from_this(), 因为构造尚未完成,实例还没 ...
- DB 查询分析器 6.04 发布 ,本人为之撰写的相关技术文章达78篇
DB查询分析器 6.04 发布,本人为之撰写的相关技术文章达78篇 中国本土程序员马根峰(CSDN专访马根峰:海量数据处理与分析大师的中国本土程序员 http://www.csdn.net/artic ...
- android实现gif动态图的使用
在android中显示一个静态图片比如png jpg等等都很方便,但是如果要显示一个gif 动态图片就需要进行一些处理. 本文是采用自定义view 然后进行重新onDraw方法来实现 首先自定义Vie ...
- 敏捷测试(2)--ATDD概念
什么是验收测试驱动开发 在准备实施一个功能或特性之前,首先团队需要定义出期望的质量标准和验收细则,以明确而且达成共识的验收测试计划(包含一系列测试场景)来驱动开发人员的TDD实践和测试人员的测试脚本开 ...
- Linux Shell 命令--rename
重命名文件,经常用到mv命令,批量重命名文件rename是最好的选择,Linux的rename 命令有两个版本,一个是C语言版本的,一个是Perl语言版本的,判断方法:输入man rename 看到第 ...
- Leetcode_101_Symmetric Tree
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/42087039 Given a binary tree, c ...