[leetcode 70]Climbing Stairs
2015-08-01 19:11
495 查看
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
相关文章推荐
- 开发并调试 Mail Add-in
- main Thread ,worker Thread
- Failed to read candidate component class
- Container With Most Water
- opencv源码分析(一):imread、cvLoadImage、waitKey、imshow函数
- Aircrack-ng 简单的WEP加密破解(上)
- 2015 Multi-University Training Contest 4(hdu5334 - Virtual Participation)数学
- HDU 2473 Junk-Mail Filter (并查集节点删除)
- activity_main cannot be resolved or is not a field
- 解决Visual Studio 2015创建工程时的“DNX SDK version 'dnx-clr-win-x86.1.0.0-beta5' failed to install.”错误
- HUAS Summer Trainning #3 C
- iWatch报错: Fail to code sign "***" No valid signing identities (i.e. certificate and private key pair
- hdu1789doing homework again【贪心】
- 2015 Multi-University Training Contest 4(HDOJ5327、5328、5335、5536)
- HUAS Summer Trainning #3 B
- Climbing Stairs
- HUAS Summer Trainning #3 A
- hdu5319 Painter
- 处理Block中的self问题(Capturing 'self' strongly in this block is likely to lead to a retain cycle)
- Paint及Canvas的简单应用