题目:

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?

题解

为了达到O(k)的空间复杂度要求,那么就要从右向左生成结果。相当于你提前把上一行的计算出来,当前行就可以用上一次计算出的结果计算了。

下面讲解转自Alf的:

“如果没有这个O(k)空间的限制,那么可以一行一行迭代生成。如果要直接生成第i行,假设生成k=3,可以这样考虑这样的一个过程:

1 0 0 0    k = 0

1 1 0 0    k = 1

1 1 1 0

1 2 1 0    k = 2

1 2 1 1

1 2 3 1

1 3 3 1    k = 3

上述过程实际上就是一个in-place的迭代过程。每当生成下一行的时候,首先数组相应位置1,然后从右向左计算每一个系数。

代码如下:

 1     public ArrayList<Integer> getRow(int rowIndex) {  
 2       ArrayList<Integer> result = new ArrayList<Integer>(rowIndex + 1);  
 3       for (int i = 0; i <= rowIndex; i++) {  
 4         result.add(0);  
 5       }  
 6       result.set(0, 1);  
 7       for (int i = 1; i <= rowIndex; i++) {  
 8         result.set(i, 1);  
 9         for (int j = i - 1; j > 0; j--) {  
           result.set(j, result.get(j) + result.get(j - 1));  
         }  
       }  
       return result;  
     } 

Reference:http://blog.csdn.net/abcbc/article/details/8982651

Pascal's Triangle II Leetcode java的更多相关文章

  1. Pascal's Triangle II —LeetCode

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...

  2. Pascal's Triangle II leetcode

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...

  3. Pascal's Triangle 2(leetcode java)

    问题描述: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return ...

  4. LeetCode算法题-Pascal's Triangle II(Java实现)

    这是悦乐书的第171次更新,第173篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第30题(顺位题号是119).给定非负索引k,其中k≤33,返回Pascal三角形的第k ...

  5. 【LeetCode】Pascal's Triangle II 解题报告

    [LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...

  6. LeetCode OJ 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, ...

  7. 学会从后往前遍历,例 [LeetCode] Pascal's Triangle II,剑指Offer 题4

    当我们需要改变数组的值时,如果从前往后遍历,有时会带来很多麻烦,比如需要插入值,导致数组平移,或者新的值覆盖了旧有的值,但旧有的值依然需要被使用.这种情况下,有时仅仅改变一下数组的遍历方向,就会避免这 ...

  8. 【LEETCODE】34、119题,Pascal's Triangle II

    package y2019.Algorithm.array; import java.util.ArrayList; import java.util.List; /** * @ProjectName ...

  9. leetcode 118. Pascal's Triangle 、119. Pascal's Triangle II 、120. Triangle

    118. Pascal's Triangle 第一种解法:比较麻烦 https://leetcode.com/problems/pascals-triangle/discuss/166279/cpp- ...

随机推荐

  1. python语法(三)— 循环

    上一篇,学习了python的判断语句,了解了python中如何直线分支语句,本文来学习循环语句.python中有两种循环while循环和for循环,当我们不知道循环次数时使用while循环,让我们知道 ...

  2. BZOJ.2716.[Violet3]天使玩偶(CDQ分治 坐标变换)

    题目链接 考虑对于两个点a,b,距离为|x[a]-x[b]|+|y[a]-y[b]|,如果a在b的右上,那我们可以把绝对值去掉,即x[a]+y[a]-(x[b]+y[b]). 即我们要求满足x[b]& ...

  3. BZOJ.1024.[SCOI2009]生日快乐(记忆化搜索)

    题目链接 搜索,枚举切的n-1刀. 对于长n宽m要切x刀,可以划分为若干个 长n'宽m'要切x'刀 的子问题,对所有子问题的答案取max 对所有子问题的方案取min 就是当前状态答案. 这显然是会有很 ...

  4. C/C++ 和 PHP 技术经典图书,学习视频资料总结

    技术经典图书 1.<计算机科学导论> 作者:(美)佛罗赞,(美)莫沙拉夫著,刘艺等译(强推) 涵盖了大部分计算机课程的内容,但都是简介,是最基础的知识,非常适合计算机初学者看,强烈建议把课 ...

  5. hadoop 视频教程2

    Hadoop大数据零基础实战培训教程 一,教程内容: 1,Hadoop2.0YARN深入浅出系列 2,Avro数据序列化系统 3,Chukwa集群监控系统 4,Flume日志收集系统 5,Greenp ...

  6. phpexcel错误 You tried to set a sheet active by the out of bounds index: 1解决办法

    $objPHPExcel->createSheet($k);

  7. ueditor上传图片设置的简单实例

    0.前言:我用过ckeditor,kingeditor还是感觉ueditor最好用,功能强大,经常更新.之前因为升级了struts2到2.5的了,原本的kingeditor已经不能共存,于是找到了ud ...

  8. 【原】使用Eclipse远程Debug测试环境

    [环境参数] Eclipse:Version: Mars.2 Release (4.5.2) Linux:centOS 6.5 [简述] Java自身支持调试功能,并提供了一个简单的调试工具--JDB ...

  9. Bitbox : a small open, DIY 32 bit VGA console

    Bitbox : a small open, DIY 32 bit VGA console Hi all, I've been developing a simple DIY console and ...

  10. Revit API布置卫浴装置

    //放置卫浴装置 [Transaction(TransactionMode.Manual)] [Regeneration(RegenerationOption.Manual)] public clas ...