LeetCode题解:Pascal's Triangle II
2015-08-12 10:13
302 查看
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行的 Pascal 三角形
解决思路:其实很简单,把 Pascal 三角形的解决办法改一改就可以了
代码:
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行的 Pascal 三角形
解决思路:其实很简单,把 Pascal 三角形的解决办法改一改就可以了
代码:
public List<Integer> getRow(int rowIndex) { List<Integer> row = new ArrayList<Integer>(); for(int i = 0;i < rowIndex + 1;++i){ row.add(0,1); for(int j = 1;j < row.size() - 1;++j){ row.set(j, row.get(j) + row.get(j + 1)); } } return row; }
相关文章推荐
- 公司培训-2015/8/11
- [bzoj2843&&bzoj1180]极地旅行社 (lct)
- 安装Python
- HTMl5中sessionStorage和localStorage的区别
- Ubuntu下使用SQLCipher命令行对db加密
- HTMLParser半自动解析网页的应用
- HDU 3397 Sequence operation(线段树·成段更新·区间合并·混合操作)
- iOS popViewControllerAnimated后刷新原先的表格
- Remove Nth Node From End of List
- oracle 学习笔记,零散知识点收集
- VB查询ACCESS数据库内容
- linux vi选中文本方法
- J2ee获取通过Nginx代理的客户端真实IP地址
- 杨氏矩阵第K小值/两个数组元素之和最小值
- 天声人語 20150812
- power of two
- 素数筛法
- css盒模型和块级、行内元素深入理解display:in
- hdu1711 KMP
- MySQL备份与恢复之真实环境使用冷备(2)