[LeetCode]Pascal's Triangle II
2016-03-15 16:40
417 查看
题目描述:
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?
解题思路:
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?
解题思路:
class Solution { public: vector<int> getRow(int rowIndex) { vector<int> result; result.push_back(1); ++rowIndex; long s = 1; if (rowIndex == 1) { return result; } for (int j = 1; j <= rowIndex - 2; ++j) { result.push_back(s = ((rowIndex - j) * s) / j); } result.push_back(1); return result; } };
相关文章推荐
- nginx配置vhost例子
- iOS 多线程 GCD的简单使用——iOS 编码复习(七)(多线程5)
- Java转换为JSON首字母大写
- 服务端备份错误的解决方法,利用fastcopy工具备份
- 双ListView显示数据
- PS设计时尚性感的派对海报
- IOS基础:声音调用
- struts2+Hibernate实现用户登陆功能
- 【bzoj3110】[Zjoi2013]K大数查询|树套树
- 应用jacob组件造成的内存溢出解决方案(java.lang.OutOfMemoryError: Java heap space)
- VS2013如何添加LIb库及头文件的步骤
- xmlWriter 以UTF-8格式写xml问题
- Mybatis学习记录(四)----resultMap的使用
- SQLServer2005 没有日志文件(*.ldf) 只有数据文件(*.mdf) 恢复数据库的方法
- 收缩SQL数据库日志文件
- 网易2016研发工程师编程题:小易的升级之路
- “Device eth0 does not seem to be present”解决办法
- mysql并发控制
- C++语言学习之对象和类
- 创建和使用自动化 Runbook