***(leetcode) Climbing Stairs
2014-09-29 09:43
357 查看
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 { /*int function(int stepsLeft){ // 递归算法,超时 if(1==stepsLeft) return 1; if(2==stepsLeft) return 2; return function(stepsLeft-1)+function(stepsLeft-2); }*/ public: int climbStairs(int n) { if(n==0) return 0; if(n==1) return 1; if(n==2) return 2; int p = 1,q=2; int tmp = 0 ; for(int i = 3 ; i <= n ; i++){ //i表示当前steps tmp = p+q; p = q ; q = tmp; } return tmp; } };
相关文章推荐
- [LeetCode]Climbing Stairs
- 【LeetCode】Climbing Stairs
- Leetcode 70 Climbing Stairs
- leetcode || 70、 Climbing Stairs
- [LeetCode] Climbing stairs 爬楼梯问题
- Leetcode_num13_Climbing Stairs
- LeetCode 70 Climbing Stairs
- LeetCode算法问题16 —— Climbing Stairs
- LeetCode 70 Climbing Stairs(Python详解及实现)
- LeetCode(70)Climbing Stairs
- LEETCODE: Climbing Stairs
- leetcode--Climbing Stairs
- leetcode-Java-70. Climbing Stairs
- LeetCode - Climbing Stairs
- LeetCode: Climbing Stairs
- 【LeetCode-动态规划】Climbing Stairs【较重要:含dp解题思路套路】
- [LeetCode] 70. Climbing Stairs
- LeetCode-70 climbing stairs(方法大总结)
- LeetCode 82 Climbing Stairs
- 刷leetcode:Climbing Stairs