[leetcode]_Pascal's Triangle II
题目:Pascal三角的变形,要求只用O(K)的额外空间。
思路:由于Pascal三角中,tri[n][i] = tri[n - 1][i] + tri[n-1][i-1],(通常情况下)
如果已经获得K = 2时的数组:
| 1 | 2 | 1 |
1、 普通思路,从前部开始,想要生成下一行,在不能申请其它辅助空间时,
| 1 | 1 | 1 |
在生成了红色的3后发现,第二个3已经无法生成了。
2、那么,考虑从数组尾部开始考虑,
| 1 | 2 | 1 |
在生成了红色3后,还能够继续往下生成,2并没有被覆盖,能够顺利得到K = 3时的结果:
| 1 | 3 | 3 | 1 |
OK,此题的解题思路,即从上一行数组的尾部开始,生成下一行数组的每一个数据,这样既不会覆盖,也能够满足O(K)的空间限制。
代码:
public List<Integer> getRow(int rowIndex) {
List<Integer> result = new ArrayList<Integer>();
for(int i = 0 ; i <= rowIndex ; i++){
result.add(1);
}
if(rowIndex == 0 || rowIndex == 1) return result;
int help = 2;
while(help <= rowIndex){
for(int i = help - 1 ; i > 0 ; i--){
result.set(i , result.get(i) + result.get(i - 1));
}
help++;
}
return result;
}
[leetcode]_Pascal's Triangle II的更多相关文章
- 学会从后往前遍历,例 [LeetCode] Pascal's Triangle II,剑指Offer 题4
当我们需要改变数组的值时,如果从前往后遍历,有时会带来很多麻烦,比如需要插入值,导致数组平移,或者新的值覆盖了旧有的值,但旧有的值依然需要被使用.这种情况下,有时仅仅改变一下数组的遍历方向,就会避免这 ...
- LeetCode: Pascal's Triangle II 解题报告
Pascal's Triangle II Total Accepted: 19384 Total Submissions: 63446 My Submissions Question Solution ...
- [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 II @ Python
原题地址:https://oj.leetcode.com/problems/pascals-triangle-ii/ 题意: Given an index k, return the kth row ...
- 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 II
Description: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3 ...
- [leetcode]_Pascal's Triangle
题目:题目本身不存在问题,生成Pascal三角. 注意: ArrayList的使用: 1.ArrayList申请二维数组. ArrayList<ArrayList<Integer>& ...
- LeetCode Pascal's Triangle II (杨辉三角)
题意:给出杨辉三角的层数k,返回最后一层.k=0时就是只有一个数字1. 思路:滚动数组计算前一半出来,返回时再复制另一半.简单但是每一句都挺长的. 0ms的版本: class Solution { p ...
- leetcode:Pascal's Triangle II【Python版】
1.将tri初始化为[1],当rowIndex=0时,return的结果是:1,而题目要求应该是:[1],故将tri初始化为[[1]],返回结果设置为tri[0]即可满足要求: 2.最开始第二层循环是 ...
随机推荐
- 配置FileZilla Ftp服务器
FileZilla是我比较喜欢用的一款FTP服务端和客户端,主要使用在Windows下,她是一款开源的FTP软件,虽然在某些功能上比不上收费软件Ser-u,但她也是一款非常好用的软件,这里主要说一下这 ...
- [Flex] ButtonBar系列——皮肤和外观设置
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="ht ...
- [Flex] ButtonBar系列——flex3 ButtonBar各项之间的间距调整
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="h ...
- Count the Colors(线段树染色)
Count the Colors Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Submit ...
- 安装LINUX X86-64的10201出现链接ins_ctx.mk错误
在安装linux X86-64的Oracle10201时,在链接过程中出现了这个错误. 详细错误信息为: Error in invoking target ‘install’ of makefile ...
- spark-sql启动后在监控页面中显示的Application Name为SparkSQL::xxxx的疑问
启动spark-sql执行sql时,在监控页面中看到该Application的Name是SparkSQL:hadoop000(其中hadoop000是测试机器的hostname),就有个想法,修改下该 ...
- android 如何设置背景的透明度
半透明<Button android:background="#e0000000" ... />透明<Button android:background=&quo ...
- 区域生长算法(附MATLAB代码实现)
一.理论概念 区域生长是按照事先定义的生长准则将一个像素或者子区域逐步聚合成一个完整独立的连通区域过程.对于图像感兴趣目标区域R,z为区域R上事先发现的种子点,按照规定的生长准则逐步将与种子点z一定邻 ...
- 【Unity Shaders】学习笔记——渲染管线
[Unity Shaders]学习笔记——Shader和渲染管线 转载请注明出处:http://www.cnblogs.com/-867259206/p/5595924.html 写作本系列文章时使用 ...
- MySQL 密码修改
方法1: 用SET PASSWORD命令 首先登录MySQL. 格式:mysql> set password for 用户名@localhost = password('新密码'); 例子:my ...