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