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 (杨辉三角之二)的更多相关文章

  1. [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 ...

  2. [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, ...

  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, ...

  4. Leetcode#118. Pascal's Triangle(杨辉三角)

    题目描述 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5 输出: [ [1], [1,1], [1,2, ...

  5. LeetCode 118. Pascal's Triangle (杨辉三角)

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...

  6. 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 ...

  7. 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: ...

  8. 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, ...

  9. 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 ...

随机推荐

  1. Java: private、protected、public和default的区别

    public: 具有最大的访问权限,可以访问任何一个在classpath下的类.接口.异常等.它往往用于对外的情况,也就是对象或类对外的一种接口的形式. protected: 主要的作用就是用来保护子 ...

  2. Java SpringMVC小白的成长(一)

    如果你是一个小白,请跟着我走,我会让你少走弯路,如果你是大牛,那么多谢大牛可以给我提提建议. 说实话,来公司这么久,一直在做的是维护与修改bug.(我的语言是php,来公司才开始接触java). 要毕 ...

  3. 时间效率:整数中1出现的次数(从1到n整数中1出现的次数)

    求出1~13的整数中1出现的次数,并算出100~1300的整数中1出现的次数?为此他特别数了一下1~13中包含1的数字有1.10.11.12.13因此共出现6次,但是对于后面问题他就没辙了.ACMer ...

  4. mysql水平分表和垂直分表的优缺点

    表分割有两种方式: 1.水平分割:根据一列或多列数据的值把数据行放到两个独立的表中. 水平分割通常在下面的情况下使用. •表很大,分割后可以降低在查询时需要读的数据和索引的页数,同时也降低了索引的层数 ...

  5. Java 异常机制

    Java 异常机制 什么是异常 异常指不期而至的各种状况,如:文件找不到.网络连接失败.非法参数等.异常是一个事件,它发生在程序运行期间,干扰了正常的指令流程 为什么要有异常 什么出错了 哪里出错了 ...

  6. PuTsangTo

    一. 跳跃与移动的优化与完善 先给上一次的内容做一次补救,也就是上一次中还留存的,由于键盘按键事件的第一次回调与后续回调之间会间隔个小半秒带来的跳跃落地后动作延迟的情况. 最终的键盘按下回调的处理代码 ...

  7. java 如何将方法作为传参--多态

    在前段时研究智能算法时,发现如果使用java进行实现的话,往往具体实现过程差不多,但是适应值函数却根据 研究对象的不同发生很大的改变,这样对代码的维护产生很大的阻碍,于是产生的一个疑问:可不可以将适 ...

  8. AngularJS的$rootScope和$scope联系和区别

    scope是html和单个controller之间的桥梁,数据绑定就靠他了. rootscope是各个controller中scope的桥梁.用rootscope定义的值,可以在各个controlle ...

  9. Hive基础(4)---Hive的内置服务

    版权声明:<—— 本文为作者呕心沥血打造,若要转载,请注明出处@http://blog.csdn.net/gamer_gyt <——   目录(?)[+]   一:Hive的几种内置服务 ...

  10. 自签名的https证书是不安全的

    一.项目内的需求 我们做的app都是企业级的应用,而企业级的应用的下载需要遵循itms协议,itms协议下需要https链接,这就需要你的服务器支持https的协议,该协议需要申请SSL证书,我们测试 ...