LeetCode 119. 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,3,1].
Note:
Could you optimize your algorithm to use only O(k) extra space?
题目标签:Array
这道题目与之前那题不同的地方在于,之前是给我们一个行数n,让我们把这几行的全部写出来,这样就可以在每写新的一行的时候根据之前的那一行来得出。这一题给了我们一个k,让我们直接得出第3行的数字(这里从0开始,所以3代表第四行)。我们可以先设一个list size为 k + 1。然后把k + 1 的0加入list。 接着设第一个位置为1,之后遍历剩下的数字,对于每一个数字,把它设为1,并且遍历这个数字的前一个数字,一直回到最开始的第二个数字,对于每一个数字,把它和它前面一个相加。(这一步就等于把之前的每一行给重现出来)。我们来看一个例子:
当k = 4,
0 0 0 0 0 先设5个0
1 0 0 0 0 第一个设为1
1 1 0 0 0 第二个设为1
1 1 0 0 第三个数字设为1的时候,就需要开始遍历前面一个数字了,因为第二个数字1 position 为1 > 0
1 2 1 0 0 遍历完之后的结果
1 2 1 1 0 第四个数字设为1的时候,需要开始遍历从前面一个数字,一直回到第二个数字
1 2 3 1 0 遍历中
1 3 3 1 0 遍历结束
1 3 3 1 1 第五个数字设为1, 从第四个遍历回第二个
1 3 3 4 1 遍历中
1 3 6 4 1 遍历中
1 4 6 4 1 遍历结束,得到答案
Java Solution:
Runtime beats 51.88%
完成日期:04/06/2017
关键词:Array
关键点:设k+1个0,设第一个为1,遍历之后的数字,把每一个数字设为1的同时,从前一个数字遍历回第二个数字,生成新一行的数组
public class Solution
{
public ArrayList<Integer> getRow(int rowIndex)
{
// define arraylist with size = rowIndex + 1.
ArrayList<Integer> result = new ArrayList<Integer>(rowIndex + 1); // first, add all 0s for each spot.
for(int i = 0; i <= rowIndex; i++)
result.add(0); // set the first number to 1.
result.set(0, 1); // iterate from second number to end
for(int i = 1; i <= rowIndex; i++)
{
// set the number to 1 first.
result.set(i, 1);
// iterate from the prior number to 2nd number (backward).
for(int j = i - 1; j > 0; j--)
result.set(j, result.get(j) + result.get(j - 1));// update the number with sum of itself and prior one. } return result;
}
}
参考资料:
http://www.cnblogs.com/springfor/p/3887913.html
LeetCode 算法题目列表 - LeetCode Algorithms Questions List
LeetCode 119. Pascal's Triangle II (杨辉三角之二)的更多相关文章
- [LeetCode] 119. Pascal's Triangle II 杨辉三角之二
Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle. Note 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, ...
- [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(杨辉三角)
题目描述 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5 输出: [ [1], [1,1], [1,2, ...
- LeetCode 118. Pascal's Triangle (杨辉三角)
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- LeetCode 119 Pascal's Triangle II
Problem: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Ret ...
- Leetcode 119 Pascal's Triangle II 数论递推
杨辉三角,这次要输出第rowIndex行 用滚动数组t进行递推 t[(i+1)%2][j] = t[i%2][j] + t[i%2][j - 1]; class Solution { public: ...
- leetcode 119 Pascal's Triangle II ----- java
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...
- Java [Leetcode 119]Pascal's Triangle II
题目描述: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return ...
随机推荐
- Easyui DataGrid DateRange Filter 漂亮实用的日期区间段筛选功能
自定义扩展Jquery easyui datagrid filter组件实现对日期类型区间段的筛选功能.显示效果如一下 是不是非常实用 引用的jquery 组件是 Date Range Picker ...
- Android 跳转系统选择本地视频的功能
今天在项目开发的过程中产品要求添加选择本地视频的功能,于是就翻阅和查找各种资料,进行功能的开发,但是在开发过程中发现,各种不同的品牌的手机跳转至系统选择本地视频的功能结果不太一样,所以我就对一些主流的 ...
- 王者荣耀_KEY
WZRY 为了排位赛的Cjj神,最近耗尽气力来打WZRY. Cjj神最近有N局预约的排位赛,其中第i局需要耗时Li的时间.因为浓浓的Gay情,Cjj神不能改变这些排位赛的的顺序.作为一个很有(mei) ...
- [err] 1055
本人mysql安装在ubuntu16.04上,mysql版本是5.7.19:在创建表和插入数据时报了 [Err] 1055 - Expression #1 of ORDER BY clause is ...
- WaitAll 和 WhenAll 的使用及区别
用过.net 异步编程的同学都知道,比以前的多线程编程实现起来真的方便很多,今天把WaitAll和WhenAll这两种编程方式回顾总结一下(当然WaitAny.WhenAny是一样的操作) 1:Wai ...
- Ubuntu16.04.1安装JDK1.8.0
今天在安装Zookeeper的时候需要安装JDK,对于.Neter来说还是有点陌生,下面我就把安装JDK的步骤记录一下,分享给大家. 一.下载JDK安装包:http://www.oracle.com/ ...
- unset与unlink
unset() -- 释放给定的变量 详见->http://www.kuqin.com/php5_doc/function.unset.html unlink() --删除文件 常用于用户 ...
- hdu1556树状数组的区间更新单点查询
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- LDA工程实践之算法篇之(一)算法实现正确性验证(转)
研究生二年级实习(2010年5月)开始,一直跟着王益(yiwang)和靳志辉(rickjin)学习LDA,包括对算法的理解.并行化和应用等等.毕业后进入了腾讯公司,也一直在从事相关工作,后边还在yiw ...
- B树、B+树、B*树
二叉搜索树 即二叉搜索树: 1.所有非叶子结点至多拥有两个儿子(Left和Right): 2.所有结点存储一个关键字: 3.非叶子结点的左指针指向小于其关键字的子树,右指针指向大于其关键字的子树: 如 ...