[leetcdoe] 119. Pascal's Triangle II 解题报告
2015-12-16 10:01
375 查看
题目链接:https://leetcode.com/problems/pascals-triangle-ii/
Given an index k, return the kth row of the Pascal's triangle.
For example, given k = 3,
Return
Note:
Could you optimize your algorithm to use only O(k) extra space?
思路:每一行除第一列和最后一列为1之外, 其他都是由其正上方一行和左边的和构成, 因此答案就很明显了
代码如下:
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?
思路:每一行除第一列和最后一列为1之外, 其他都是由其正上方一行和左边的和构成, 因此答案就很明显了
代码如下:
class Solution { public: vector<int> getRow(int rowIndex) { vector<int> vec(rowIndex+1, 1); for(int i = 2; i <= rowIndex; i++) for(int j = i-1; j>=1; j--) vec[j] = vec[j] + vec[j-1]; return vec; } };
相关文章推荐
- linux设备驱动程序第四部分:从如何定位oops对代码的调试方法,驱动线
- 将博客搬至CSDN
- LeetCode 3 Longest Substring Without Repeating Characters(最长不重复子序列)
- Ajax中 async的作用
- 【详解】简单验证码的解析
- Android通过startService实现文件批量下载
- OC_04_03
- vaadin系列(一)框架和spring整合
- 这是什么原因啊
- MFC关于VS2013内存释放的定位
- android TextView中间删除线显示
- Solr配置与简单Demo
- OC_04_02
- javascript函数式编程要掌握的知识点讲解
- OC学习篇之---Foundation框架中的NSArray类和NSMutableArray类
- 万年历---java版
- VNCServer 配置
- 使用visio 提示此UML形状所在的绘图页不是UML模型图的一部分 请问这个问题怎么解决?
- OC_04_01
- [No000063]Python格式化输出