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 ...
随机推荐
- 学习Tensorflow,使用源码安装
PC上装好Ubuntu系统,我们一步一步来讲解如何使用源码安装tensorflow?(我的Ubuntu系统是15.10) 安装cuda 根据你的系统型号选择相应的cuda版本下载 https://de ...
- 学习TensorFlow,线性回归模型
学习TensorFlow,在MNIST数据集上建立softmax回归模型并测试 一.代码 <span style="font-size:18px;">from tens ...
- Android程序崩溃异常处理框架
目前我已经把框架放在了github了,地址如下:https://github.com/msdx/android-crash 使用方法见github上面的README.md. 本项目相关jar包已发布在 ...
- Cookie 进阶
Cookie作为一个客户端技术被广泛的应用着.我今天也来谈一谈我对Cookie的理解. 先来一个小菜(实现"上次登录时间") 具体的思路如下: 通过request.getCooki ...
- Redefine:Change in the Changing World
EMC World 2014的主题就是REDEFINE.的确,现在科技的发展在重新定义了技术,影响了生活,改变了你我. 对于一个有数万员工,甚至数十万员工的企业来说,Redefine无疑更加具有挑战, ...
- Oracle Metalink Notes Collection
INV Note 123456.1 Latest 11i Applications Recommended Patch List Note 568012.1:FAQ: Inventory Standa ...
- 【嵌入式开发】 嵌入式开发工具简介 (裸板调试示例 | 交叉工具链 | Makefile | 链接器脚本 | eclipse JLink 调试环境)
作者 : 韩曙亮 博客地址 : http://blog.csdn.net/shulianghan/article/details/42239705 参考博客 : [嵌入式开发]嵌入式 开发环境 (远 ...
- A*寻路算法入门(三)
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...
- xpath技术解析xml以及案例模拟用户登录效果
问题:当使用dom4j查询比较深的层次结构的节点(标签,属性,文本),比较麻烦!!! xpath就在此情况下产生了--主要是用于快速获取所需的[节点对象]. 在dom4j中如何使用xPath技术 1) ...
- Cocos2D:塔防游戏制作之旅(七)
用这3个变量,你可以创建多种不同类型的炮塔,它们可以有着不同的攻击属性,比如长距离重型攻击力,但是慢速攻击的炮塔,或者是渴望快速攻击但是攻击范围近的炮塔. 最后,代码包括了一个draw方法,它在炮塔周 ...