70. Climbing Stairs
2016-04-19 16:31
483 查看
70. Climbing Stairs
这个问题抽象出来就是求解费波拉契数列。而求解费波拉契数列一般两种方法,一种就是基于循环的,一种是基于递归的。虽然这题用递归解比较简单直观,但是当n较大的时候,程序效率非常低,结果甚至报错;所以从效率上考虑,一般建议使用循环。
基于循环:
基于递归
这个问题抽象出来就是求解费波拉契数列。而求解费波拉契数列一般两种方法,一种就是基于循环的,一种是基于递归的。虽然这题用递归解比较简单直观,但是当n较大的时候,程序效率非常低,结果甚至报错;所以从效率上考虑,一般建议使用循环。
基于循环:
class Solution { public: int climbStairs(int n) { int result[2]={1,1}; if(n<2) return result ; long long f1=1; long long f2=1; long long f=0; for(int i=2;i<=n;i++) { f=f1+f2; f1=f2; f2=f; } return f; } };
基于递归
class Solution { public: int climbStairs(int n) { int result[2]={1,1}; if(n<2) return result ; return climbStairs(n-1)+climbStairs(n-2); } };
相关文章推荐
- ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction(死锁)
- 11. Container With Most Water
- HDU 1104 remainder
- SFTP 上传文件报错:SFTP ERROR?Algorithm negotiation fail
- ERROR 1215 (HY000): Cannot add foreign key constraint 解决方法
- [2016-04-19 15:46:03 - IceHoloReader1.0] Installation error: INSTALL_FAILED_CONFLICTING_PROVIDER [20
- Jenkins进阶系列之——02email-ext邮件通知模板
- AssertionFailedError
- 如何用Paint实现文字居中?
- Failed to initialize end point associated with ProtocolHandler
- Jenkins进阶系列之——01使用email-ext替换Jenkins的默认邮件通知
- monodb因 /usr/lib64/libssl.so.10: no version information available原因不能连接的问题
- No valid signing identities (i.e. certificate and private key pair) matching the team ID “(null)” were found.
- Paint及Canvas的简单应用
- light Oj 1071 - Baker Vai(记忆化搜索)
- hdoj 1789 Doing Homework again
- 提示:Failure [INSTALL_FAILED_ALREADY_EXISTS]
- unresolved external symbol _Winmain@16问题解决办法
- 设计模式总结之职责链模式 (Chain of responsibility)
- 为什么要内存对齐 Data alignment: Straighten up and fly right