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 ...
随机推荐
- appium_python_android测试环境搭建
第一步 安装appium •Appium是由.NET 开发的,所以,它会依赖 .NET framework相关组件,所以先安装.net framework 4.5,备注: Appium最低支持.ne ...
- Python数据库(二)-Mysql数据库插入数据
通过python连接mysql数据库,并插入数据 # -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" import pymysql ...
- Oracle 在约束中使用正则表达式
ALTER TABLE mytest ADD CONSTRAINT CK_REG CHECK(REGEXP_LIKE(TEST, '^[0-9]{1,3}(\.[0-9]){0,1}$'));
- 10-17C#语句(3)--跳转语句、异常处理语句
回顾: 穷举法(重点掌握):虽然运用for...嵌循环语句,但是也要找到执行for...循环的规律, 即一个题目中,需要得到哪个值,首先定义它初始变量:哪个条件需要改变,它对应的就是for...循环的 ...
- git clone 某一特定分支<转>
网上搜索自己想要的答案,往往会搜大一大堆感觉没用的,或者看不懂的东西, 最好终于找到了想要答案,特记录一下: ============================================= ...
- css之content
content 属性与 :before 及 :after 伪元素配合使用,来插入生成内容.该属性用于定义元素之前或之后放置的生成内容.默认地,这往往是行内内容,不过该内容创建的框类型可以用属性 dis ...
- MyBatis配置Setting详细说明
该表格转载自http://blog.csdn.net/summer_yuxia/article/details/53169227 setting是指定MyBatis的一些全局配置属性,这是MyBati ...
- 数组中的最大值以及最小值的位置变换的问题(C++)
将一个5×5的数组中的最大值放到数组的中心位置 分析:遍历数组,找到最大的元素,然后将该元素与中心位置的元素交换位置 #include<iostream> #include <std ...
- (转)嵌入式C开发人员的最好笔试题目
约定: 1) 下面的测试题中,认为所有必须的头文件都已经正确的包含了 2)数据类型 char 一个字节 1 byte int 两个字节 2 byte ( ...
- 100723H Obfuscation
传送门 题目大意 给你一个包含n 个单词的字典,给你一篇文章,文章包括若干词典里的单词,把句子里的空格都去掉,单词的首位字母都不变,中间的字符集为乱序,问能否恢复这篇文章,使得单词都是词典里的单词,如 ...