[leetcode] 70. Climbing Stairs
2016-03-22 15:12
351 查看
You are climbing a stair case. It takes n steps to reach to the top.
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
用递归,显示运行超时。(很多运算重复了)
用数组保存,节省堆栈资源
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
class Solution { public: int climbStairs(int n) { if(n==1) return 1; if(n==2) return 2; return climbStairs(n-1)+climbStairs(n-2); } };
用递归,显示运行超时。(很多运算重复了)
class Solution { public: int climbStairs(int n) { if(n<=2) return n; int st ; st[0]=1; st[1]=2; int i; for(i=2;i<n;i++) { st[i]=st[i-1]+st[i-2]; } return st[n-1]; } };
用数组保存,节省堆栈资源
相关文章推荐
- hdu 1867 A + B for you again
- AIDL进程间通信
- Delete Duplicate Emails
- Duplicate Emails
- shared_ptr的一些尴尬 http://blog.csdn.net/henan_lujun/article/details/8984543
- svnmanager:You entered an invalid email address.
- 【uva11374】Airport Express 最短路
- 查找行迁移及消除行迁移(chained rows)
- 韦玮:解决Ubuntu的root账号无法登录SSH问题-Permission denied, please try again.
- maven中指定main方法并且导入关联jar包
- Wait和Sleep的区别
- 人工智能的突破需要颠覆图灵机吗?
- 人工智能
- 2016-03-22 OneZero团队 Daily Scrum Meeting
- My Dad said
- NEU 1685: All Pair Shortest Path
- 【杭电oj】1787 - GCD Again(欧拉函数)
- 你真的了解人工智能吗?——聊聊AI的碰壁和冬天
- 220. Contains Duplicate III
- http://blog.csdn.net/lmj623565791/article/details/50709663