LeetCode--Climbing Stairs
2015-01-12 16:55
225 查看
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?
递推公式T(n) = T(n-1) + T(n-2);
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
递推公式T(n) = T(n-1) + T(n-2);
class Solution { public: int climbStairs(int n) { int pre; int last; if(n==0) return 0; if(n == 1) return 1; pre=1; last=1; int res=0; for(int i=2; i<=n; i++) { res = pre+last; pre = last; last = res; } return res; } };
相关文章推荐
- LeetCode-Climbing Stairs
- [Leetcode] Climbing Stairs
- 算法分析与设计课程——LeetCode刷题之 Climbing Stairs
- 【Leetcode】Climbing Stairs
- leetcode-70 Climbing Stairs
- [leetcode] Climbing Stairs
- Leetcode: Climbing Stairs
- LeetCode:Climbing Stairs
- LeetCode: Climbing Stairs
- Leetcode-Climbing Stairs
- leetcode[70]:Climbing Stairs
- Leetcode-70 Climbing Stairs
- 【LeetCode】Climbing Stairs
- LEETCODE-Climbing Stairs
- leetcode [Climbing Stairs]
- Leetcode-70: Climbing Stairs (DP, 尾递归,Memorization)
- Leetcode-Climbing Stairs
- LeetCode70 Climbing Stairs
- LeetCode:Climbing Stairs
- leetcode 70-Climbing Stairs