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 ...
随机推荐
- 用户登陆注册【JDBC版】
前言 在讲解Web开发模式的时候,曾经写过XML版的用户登陆注册案例!现在在原有的项目上,使用数据库版来完成用户的登陆注册!如果不了解的朋友,可以看看我Web开发模式的博文! 本来使用的是XML文件作 ...
- Javascript跳转页面和打开新窗口等方法
1.在原来的窗体中直接跳转用onClick="window.location.href='你所要跳转的页面';" 2.在新窗体中打开页面用:onclick="window ...
- Linux 启动详解之init
1.init初探 init是Linux系统操作中不可缺少的程序之一.init进程,它是一个由内核启动的用户级进程,然后由它来启动后面的任务,包括多用户环境,网络等. 内核会在过去曾使用过init的几个 ...
- 程序编译没错,运行报错:无法定位程序输入点GT_BufLaserFollowRatio(这是函数)于动态链接库GTS.DLL上
:DLL里面没有导出该函数 :DLL没放进DEBUGS文件夹 (当时的情况是这个)
- redis C接口hiredis 简单函数使用介绍
hiredis是redis数据库的C接口,目前只能在linux下使用,几个基本的函数就可以操作redis数据库了. 函数原型:redisContext *redisConnect(const char ...
- Crossin-8-1;8-2课程记录
打开文件: open,注意打开文件的路径 读取结束需使用close读取文件: read readlines readline for in 重置光标位置: se ...
- webpack2使用ch6-babel使用 处理es6 优化编译速度
1 目录结构 安装依赖 cnpm install --save-dev babel-loader babel-core babel-preset-env babel-preset-latest &qu ...
- java Web Servlet学习笔记-1 HttpServletQequest对象的学习
HttpServletQequest对象的学习 HttpServletRequest HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP请求 ...
- Java基础语法(下篇)
Java基础语法(下篇) 内容概要: (1)函数的定义 (2)函数的特点 (3)函数的应用 (4)函数的重载 ...
- WebService文件上传相关配置(404.13、超出限制、超时)
最近在做文件上传的功能,遇到一些问题,记录如下,以备以后使用. 1.HTTP Error 404.13 - Not Found,请求筛选模块被配置为拒绝超过请求内容长度的请求. IIS默认允许请求长度 ...