[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.最开始第二层循环是 ...
随机推荐
- 如何构建日均千万PV Web站点
http://www.cnblogs.com/xiaocen/p/3723839.html http://www.cnblogs.com/xiaocen/p/3726763.html http://w ...
- spring整合struts
整合目标:使用spring的bean管理struts action service. 整合步骤: 一.加入spring 1.加入spring jar包 2.配置web.xml文件 <contex ...
- [Java] 01 String 内存分析
public class StringTest{ public static void main(String[] args){ String str1 = new String("123& ...
- 反人类的MyEclipse之-eclipse设置花括号换行显示
http://www.cnblogs.com/zhwl/archive/2012/12/17/2821806.html 习惯了C的代码风格,用Eclipse的风格,实在是看得卵子痛.尤其是大括号放在最 ...
- HTTP权威指南之连接管理
TCP连接世界上几乎所有的 HTTP 通信都是由 TCP/IP 承载的, TCP/IP 是全球计算机及网络设备都在使用的一种常用的分组交换网络分层协议集. 客户端应用程序可以打开一条 TCP/IP 连 ...
- Ext.MessageBox
Ext.require([ 'Ext.window.MessageBox', 'Ext.tip.*' ]); Ext.onReady(function(){ Ext.MessageBox.confir ...
- (easy)LeetCode 231.Power of Two
Given an integer, write a function to determine if it is a power of two. Credits:Special thanks to @ ...
- [HDU 5074] Hatsune Miku (动态规划)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5074 题目大意是给你m个note,n个数,得分是v[a[i]][a[i+1]]的总和,如果说a[i]是 ...
- var obj = {};var obj2 = [];var obj3;
<script> var obj = {}; console.log(obj); var obj2 = []; console.log(obj2); var obj3; console.l ...
- Hibernate高级查询QBC条件设置——Restrictions用法 引自:http://www.cnblogs.com/evon168/archive/2010/10/29/1863059.html
方法说明 方法 说明 Restrictions.eq = Restrictions.allEq 利用Map来进行多个等于的限制 Restrictions.gt > Restrictions.ge ...