题目:
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?

翻译:
给定一个下标k,返回第k行的杨辉三角。
例如给定k=3,返回[1,3,3,1]。
提示:你可以优化你的算法,让它只使用O(k)的额外空间吗?

分析:
如果采用Leet Code OJ 118. Pascal’s Triangle中的方案来做这道题,也就是使用2个Integer数组进行存储,空间复杂度是O(2k),实际是等价于O(k)的,但是我们考虑一下能否再优化一些。下面的方案去掉lastLine这个数组,采用2个整数代替,空间复杂度为O(k+2),略优于之前方案。

Java版代码:

public class Solution {public List<Integer> getRow(int rowIndex) {List<Integer> line=new ArrayList<>();line.add(1);if(rowIndex==0){return line;}for(int i=1;i<=rowIndex;i++){int lastNum=1;int currentNum=1;for(int j=1;j<i;j++){currentNum=line.get(j);line.set(j,lastNum+currentNum);lastNum=currentNum;}line.add(1);}return line;}
}

Leet Code OJ 119. Pascal's Triangle II [Difficulty: Easy]相关推荐

  1. Leet Code OJ 118. Pascal's Triangle [Difficulty: Easy]

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

  2. Leet Code OJ 8. String to Integer (atoi) [Difficulty: Easy]

    题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...

  3. Leet Code OJ 203. Remove Linked List Elements [Difficulty: Easy]

    题目: Remove all elements from a linked list of integers that have value val. Example Given: 1 –> 2 ...

  4. Leet Code OJ 191. Number of 1 Bits [Difficulty: Easy]

    题目: Write a function that takes an unsigned integer and returns the number of '1' bits it has (also ...

  5. Leet Code OJ 328. Odd Even Linked List [Difficulty: Easy]

    题目: Given a singly linked list, group all odd nodes together followed by the even nodes. Please note ...

  6. Leet Code OJ 58. Length of Last Word [Difficulty: Easy]

    题目: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return t ...

  7. Leet Code OJ 21. Merge Two Sorted Lists [Difficulty: Easy]

    题目: Merge two sorted linked lists and return it as a new list. The new list should be made by splici ...

  8. Leet Code OJ 168. Excel Sheet Column Title [Difficulty: Easy]

    题目: Given a positive integer, return its corresponding column title as appear in an Excel sheet. For ...

  9. Leet Code OJ 171. Excel Sheet Column Number [Difficulty: Easy]

    题目: Related to question Excel Sheet Column Title Given a column title as appear in an Excel sheet, r ...

最新文章

  1. gff3转mysql_五月 | 2013 | 陈连福的生信博客
  2. 在你做回归测试的时候,突然有个着急的测试需求,你会怎么做?
  3. 招募 | 清华大学计算机系副教授黄民烈招募NLP方向博士后
  4. Python第二十天 shutil 模块 zipfile tarfile 模块
  5. ubuntu下面的背光键盘的使用
  6. tomcat java环境配置
  7. oracle进程用户,关于oracle用户进程,服务进程,后…
  8. Java基础学习总结(34)——HTTP协议详解
  9. 医学生可以跨专业考计算机的专业,可以跨考医学研究生:2016跨专业考研需谨慎的专业解读:临床医学...
  10. kdj买卖指标公式源码_八字kdj股票技术指标公式源码(图解)
  11. 微信小程序短视频去水印解析
  12. 电气-NPN、PNP传感器应用
  13. 拉普拉斯矩阵与正则化
  14. 衰老,其实是一种疾病?
  15. IDEA 神级插件!效率提升 50 倍!
  16. 第五章--设备内容(The Device Context)(2)
  17. 服务器电话销售话术,电话销售必看:让客户无法拒绝的13个经典话术
  18. Map阶段环形缓冲区详细分析
  19. 各大高校自曝状态一览,排名不分先后
  20. cgb2111-day01

热门文章

  1. AJAX DELETE
  2. C++中ASCII、unicode与Utf8之间的相互转化
  3. cocos2d-x游戏实例(4)-地图碰撞
  4. 网狐棋牌(五) TCPSocketEnging分析
  5. Harfbuzz API 基本用法
  6. LevelDB是什么?为什么我们需要K-V存储?
  7. docker学习笔记(一)docker入门
  8. 网络协议,我明明学过的呀?
  9. 火山引擎端云一体化服务:打造面向体验的视频云
  10. 客观指标VS主观质量:拨开遮挡高清视频技术的迷雾