【LeetCode】Climbing Stairs
2014-04-17 10:57
441 查看
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?
思路:斐波拉契数
f(n)=f(n-1)+f(n-2)(n>2),f(0)=1;f(1)=1;f(2)=2.
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
思路:斐波拉契数
f(n)=f(n-1)+f(n-2)(n>2),f(0)=1;f(1)=1;f(2)=2.
class Solution { public: int climbStairs(int n) { int result[]={0,1,2}; if(n<=2){ return result ; } int prenum=result[2],frontnum=result[1],tmp; for(int i=3;i<=n;i++){ tmp=prenum; prenum+=frontnum; frontnum=tmp; } return prenum; } };
相关文章推荐
- [Leetcode 70, easy] Climbing Stairs
- LeetCode:Climbing Stairs
- [LeetCode] Climbing Stairs
- 【LeetCode】遇到一个很有趣的问题:Climbing Stairs
- LeetCode 070 Climbing stairs
- leetcode(20).70. Climbing Stairs
- LeetCode: 013-Climbing Stairs
- LeetCode | Climbing Stairs
- [LeetCode] Climbing Stairs (Sequence DP)
- LeetCode-Climbing Stairs
- Leetcode: Climbing stairs
- 【leetcode】Climbing Stairs
- 【Leetcode】【python】Climbing Stairs
- leetcode:Climbing Stairs
- leetcode 107: Climbing Stairs
- LeetCode_Climbing Stairs
- leetCode: Climbing Stairs
- Leetcode: Climbing Stairs
- LeetCode:Plus One, Add Binary, Sqrt(x), Climbing Stairs
- 【LeetCode with Python】 Climbing Stairs