LeetCode_climbing-stairs
2015-09-10 10:07
417 查看
原题链接:https://leetcode.com/problems/climbing-stairs/
同斐波那契数列: class Solution { public: int climbStairs(int n) { int s1 = 1,s2 = 2,s3; if(n == s1) return s1; if(n == s2) return s2; for(int i = 3;i <= n;i++) { s3 = s2 + s1; s1 = s2; s2 = s3; } return s3; } }; 递归解法: #include <iostream> using namespace std; void climbWays(int n,int &cnt,int curSteps) { if(curSteps > n) return; if(curSteps == n) { cnt++; return; } climbWays(n,cnt,curSteps+1); climbWays(n,cnt,curSteps+2); } int climbStairs(int n) { int cnt = 0; int curSteps = 0; climbWays(n,cnt,curSteps); return cnt; } int main(int argc, char *argv[]) { cout << climbStairs(10) << endl; cout << climbStairs(15) << endl; cout << climbStairs(20) << endl; cout << climbStairs(25) << endl; cout << climbStairs(30) << endl; return 0; }
相关文章推荐
- PopupWindow$PopupViewContainer.dispatchKeyEvent
- 什么是trainingset ,validateion set ,test set
- org.tinygroup.beancontainer-IOC、AOP框架
- 抛出错误Debug Assertion Failed!
- [LeetCode] Container With Most Water
- FZU 2156 Climb Stairs
- NSSearchPathForDirectoriesInDomains用法
- Get the date of the days before today on AIX
- HDU 1789 Doing Homework again
- 使用GCE(Google Container Engine)体验容器云服务 (1)
- VMware Containers - VMware vSphere Integrated Containers
- 游戏人工智能开发之6种决策方法
- Ubuntu下出现su:authentication failure的解决办法
- 快速去除图标斜线水印
- dnsmasq+postfix+sasl+tls+dovecot(邮件服务器)
- traits:Traits技术学习笔记
- cocos2d智能指针 转自:http://blog.csdn.net/nxshow/article/details/44699409
- hdu 2686 Matrix / 3376 Matrix Again最大费用流
- Waiting for table metadata lock问题的解决
- 第24/24周 数据库维护(Database Maintenance)