Leetcode 70. Climbing Stairs 爬楼梯 (递归,记忆化,动态规划)
题目描述
要爬N阶楼梯,每次你可以走一阶或者两阶,问到N阶有多少种走法
测试样例
Input: 2
Output: 2 Explanation: 到第二阶有2种走法
1. 1 步 + 1 步
2. 2 步 Input: 3
Output: 3
Explanation: 到第三阶有3种走法
1. 1 步 + 1 步 + 1 步
2. 1 步 + 2 步
3. 2 步 + 1 步
详细分析
在第0阶,可以选择走到第1阶或者第2阶,第1阶可以走第2阶或者第3阶,第二阶可以走第3阶或者第4阶...。如此继续就生成了上图递归解答树。注意如果直接递归会超时,当前实现使用了记忆化储存子解。
算法实现
记忆化递归(√)
class Solution {
public:
int climbStairs(int n) {
this->n = n;
memo = new int[n+];
for(int i=;i<n+;i++){
memo[i] = -;
}
return recursiveClimbing();
}
int recursiveClimbing(int currentStep){
if(memo[currentStep]!=-){
return memo[currentStep];
}
if(currentStep==n){
return ;
}
if(currentStep>n){
return ;
}
memo[currentStep] = recursiveClimbing(currentStep+) + recursiveClimbing(currentStep+);
return memo[currentStep];
}
private:
int n;
int total = ;
int *memo;
};
Leetcode 70. Climbing Stairs 爬楼梯 (递归,记忆化,动态规划)的更多相关文章
- [LeetCode] 70. Climbing Stairs 爬楼梯问题
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- [LeetCode] 70. Climbing Stairs 爬楼梯
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- LeetCode 70. Climbing Stairs爬楼梯 (C++)
题目: You are climbing a stair case. It takes n steps to reach to the top. Each time you can either cl ...
- [leetcode]70. Climbing Stairs爬楼梯
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- 70. Climbing Stairs爬楼梯
网址:https://leetcode.com/problems/climbing-stairs/ 其实就是斐波那契数列,没什么好说的. 注意使用3个变量,而不是数组,可以节约空间. class So ...
- Leetcode#70. Climbing Stairs(爬楼梯)
题目描述 假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数. 示例 1: 输入: 2 输出: 2 解 ...
- 42. leetcode 70. Climbing Stairs
70. Climbing Stairs You are climbing a stair case. It takes n steps to reach to the top. Each time y ...
- LN : leetcode 70 Climbing Stairs
lc 70 Climbing Stairs 70 Climbing Stairs You are climbing a stair case. It takes n steps to reach to ...
- leetCode 70.Climbing Stairs (爬楼梯) 解题思路和方法
Climbing Stairs You are climbing a stair case. It takes n steps to reach to the top. Each time you ...
随机推荐
- [置顶]
从零制作文件系统到JZ2440,使其支持telnet , ftp 和tftp
转自:http://mp.weixin.qq.com/s?__biz=MzAxNTAyOTczMw==&mid=2649328515&idx=1&sn=5849fba4b44e ...
- arm开发板6410/2440上mjpg-streamer网络视频服务器移植
摄像头移植 一.环境 主机环境 :ubuntu 10.10 目标板 :FS-S5PC100 主机工具链 :gcc-4.4.5 交叉工具链 :arm-unknown-li ...
- 新建 FrameMaker API 时引用目录的设置
如果将FDK安装目录下的Sample项目拷贝到其它目录编译,往往会报错 c1083 找不到fapi.h等头文件,或者Link时报错找不到.lib文件. 1.可通过菜单-项目-xxx属性-配置属性-c/ ...
- java常用八大排序法
最近查资料发现java排序挺有意思的,其中包含常见八种具有代表性的排序法:笔者觉得排序的功能重要,但更重要的是排序的思想:所以简单叙述一下常见排序方法名称,并用代码举例. A.插入排序(直接插入排序. ...
- re.I re.L re.M re.S re.U re.X
- [patl1-046]整除光棍
解题关键:模拟除法 #include<cstdio> #include<cstring> #include<algorithm> #include<cstdl ...
- acrord32 pdf自动化
这个东西就是帮助用户自动化处理pdf的 . 可以看作是adobe reader的命令行
- RTX这种东西究竟有什么价值?
我在第一家公司工作的时候,同事沟通用的就是RTX,第一感觉就是这么简单的软件也能卖钱? 这种东西有啥价值啊?不就是个没广告蓝色UI的qq吗? 还是那句话,当你已经习惯了一个东西之后,你不会感觉到他的价 ...
- R: 基本的数学运算
################################################### 问题:基本数学运算 18.4.30 R语言用于初等数学的计算,都怎么表示??加减乘除.余数. ...
- Entity Framework Tutorial Basics(35):Local Data
Local Data The Local property of DBSet provides simple access to the entities that are currently bei ...
