[leetcode 70]Climbing Stairs
2017-05-28 14:46
447 查看
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?
爬台阶,你每次能够上一层或者两层,问上n层你共同拥有多少何种方法
f(n)=f(n-1)+f(n-2)
动规,记得保存n-1和n-2的结果,直接使用递归会超时
AC代码
class Solution {
public:
int climbStairs(int n) {
int count[n+1];
count[0]=0;
count[1]=1;
count[2]=2;
if(n==1)
return 1;
if(n==2)
return 2;
for(int i=3;i<=n;++i)
count[i]=count[i-1]+count[i-2];
return count
;
}
};
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
爬台阶,你每次能够上一层或者两层,问上n层你共同拥有多少何种方法
f(n)=f(n-1)+f(n-2)
动规,记得保存n-1和n-2的结果,直接使用递归会超时
AC代码
class Solution {
public:
int climbStairs(int n) {
int count[n+1];
count[0]=0;
count[1]=1;
count[2]=2;
if(n==1)
return 1;
if(n==2)
return 2;
for(int i=3;i<=n;++i)
count[i]=count[i-1]+count[i-2];
return count
;
}
};
其它Leetcode题目AC代码:https://github.com/PoughER/leetcode
相关文章推荐
- leetcode_question_70 Climbing Stairs
- [leetcode 70] Climbing Stairs
- LeetCode 70 Climbing Stairs(爬楼梯)(动态规划)(*)
- LeetCode70 Climbing Stairs
- LeetCode-70 climbing stairs(方法大总结)
- LeetCode 70 Climbing Stairs
- leetcode_question_70 Climbing Stairs
- (java)leetcode-70:Climbing Stairs
- LeetCode-70-Climbing Stairs(动态规划)-Easy
- LeetCode 70 Climbing Stairs(记忆化搜索)
- LeetCode 70:Climbing Stairs
- [Leetcode 70, easy] Climbing Stairs
- Java [Leetcode 70]Climbing Stairs
- [leetcode 70]Climbing Stairs
- LeetCode70 Climbing Stairs
- LeetCode 70 Climbing Stairs--Python实现
- LeetCode70 Climbing Stairs
- 【LEETCODE】70-Climbing Stairs
- Climbing Stairs | leetcode 70 【Java解题报告】
- [LeetCode-70] Climbing Stairs