Pascal's Triangle II
2015-08-06 21:28
288 查看
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是从0开始的,第1行是[1,1],之后从后向前计算上一行两个数的和。
For example, given k = 3,
Return [1,3,3,1].
Note:
Could you optimize your algorithm to use only O(k) extra space?
k是从0开始的,第1行是[1,1],之后从后向前计算上一行两个数的和。
public class Solution { public List<Integer> getRow(int rowIndex) { List<Integer> result=new ArrayList<Integer>(); if(rowIndex<0) return result; result.add(1); for(int i=1;i<=rowIndex;i++){ for(int j=result.size()-2;j>=0;j--) result.set(j+1,result.get(j)+result.get(j+1)); result.add(1); } return result; } }
相关文章推荐
- LeetCode(116) Populating Next Right Pointers in Each Node
- 智慧城市
- 黑马程序员--学习OC加强内存管理
- hdu-3008魔兽争霸
- 全排列
- UE4 iOS设备的兼容性
- [译]Front-end-Developer-Interview-Questions
- JDK动态代理机制
- java--面向对象(3)--黑马程序员
- Ubuntu Server 14.04 & Apache2.4 虚拟主机、模块重写、隐藏入口文件配置
- Power oj 1782
- 九度oj 1335
- mysql 高可用 1
- C - Valera and Contest题解
- apk文件反编译工具apktool的下载,安装,使用
- [Learning Linux For Fun ] unit 1 1.1
- 网页制作之JavaScript部分3--事件及事件传输方式(函数调用 练习题 )重要---持续更新中
- uva 10670 Work Reduction(贪心)
- 对于HTML的一些不熟悉的及要点掌握
- Web页面优化