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 ...
随机推荐
- Hyperledger Fabric 1.0 从零开始(四)——Fabric源码及镜像文件处理
2:Fabric源码及镜像文件处理 2.1下载Fabric源码 下载Fabric源码是因为要用到源码中提到的列子和工具,工具编译需要用到go语言环境,因此需要把源码目录放到$GOPATH下.通过1.3 ...
- Android Studio不更新到最新版使用Kotlin
第一步:安装Kotlin插件 打开Settings面板,找到Plugins选项,点击Browse repositories(浏览仓库),输入“Kotlin”查找,然后安装即可.安装完成之后需要重启An ...
- hadoop-2.6.0源码编译问题汇总
在上一篇文章中,介绍了hadoop-2.6.0源码编译的一般流程,因个人计算机环境的不同, 编译过程中难免会出现一些错误,下面是我编译过程中遇到的错误. 列举出来并附上我解决此错误的方法,希望对大家有 ...
- HashMap 学习心得
1.构造 HashMap 底层数据结构线性数组,HashMap有一个静态内部类Entry,Entry有四个属性,key,value,next,hash Entry就是HashMap键值对实现的一个基础 ...
- 利用Struts拦截器限制上传图片的格式和大小
在这之前 Struts的一个核心功能就是大量的拦截器,既然是框架,那么自然也就贴心地为我们准备好了各种常用的功能,比如这里即将讨论的如何限制上传图片的格式和大小.那么既然是使用Struts已经写好的拦 ...
- Linux 文件系统的组织方式
在linux中,一切都看成文件,不管是目录还是设备.所以想要熟练使用Linux,那么对文件的本质和组织结构要有一定的认识. linux系统下使用的设备不用像windows系统下一样创建驱动器磁盘符.l ...
- sourcetree和gitlab配置图解
一.前期准备安装 1.git客户端(1.产生gitlab服务端和本地git相互传输时所需要校验的私钥和公钥 2.直接在Idea中使用git提交和push代码,当然也可以用sourcetree提交 ...
- 传统编程和IoC的对比
ref:http://www.importnew.com/13619.html 传统编程:决定使用哪个具体的实现类的控制权在调用类本身,在编译阶段就确定了. IoC模式:调用类只依赖接口,而不依赖具体 ...
- Node.js 异步异闻录
本文首发在个人博客:http://muyunyun.cn/posts/7b9fdc87/ 提到 Node.js, 我们脑海就会浮现异步.非阻塞.单线程等关键词,进一步我们还会想到 buffer.模块机 ...
- webpack2进阶之多文件,DLL,以及webpack-merge
本需要对webpack已有一定的了解,如果你没接触过webpack或者刚刚接触webpack,可以考虑先看一下我的这篇教程. 入门教程 1.打包多文件 之前,当需要打包多个而文件时,我是这么写的: m ...