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 ...
随机推荐
- Android SDK下载项的说明
Tools下 1.android sdk tools 软件开发工具包(software development kit):包括测试.调试.第三方工具.模拟器.数据管理工具等. 2.android sd ...
- react-router4.x 组件和api介绍
react-router实用4.2.0 react-router非常复杂整体,比vue-router强大很多,好好研究,对你自身能力提高很有帮助 安装 cnpm install react-route ...
- django的render的说明
return render(request,"homesite.html",locals()) homesite.html页面中的所有内容都可以被渲染,不论是标签还是js代码,包括 ...
- request 10.0 模块安装
https://pypi.python.org/packages/49/6f/183063f01aae1e025cf0130772b55848750a2f3a89bfa11b385b35d7329d ...
- JAVA基础知识总结14(String、StringBuffer、StringBuilder)
1.String字符串: java中用String类进行描述.对字符串进行了对象的封装.这样的好处是可以对字符串这种常见数据进行方便的操作.对象封装后,可以定义N多属性和行为. 如何定义字符串对象呢? ...
- bzoj 2653 middle(主席树)
题面:https://vjudge.net/problem/HYSBZ-2653 博客:https://blog.csdn.net/litble/article/details/78984846 这个 ...
- C++的引用的使用
1引用的定义 引用时C++对C的一个重要的扩充,引用的作用是给变量起一个别名. 例如: int a; int &b=a;//声明b是a的引用 经过以上的声明,b就成为了a的别名,a和b的地位以 ...
- vray学习笔记(2)vray工作流程
在bilibili上面搜索到了一个vray的教程,虽然是英语的,细节方面可能听不太懂,但可以了解整个工作流程,工作流程太重要了,先看下视频的目录: 第1节到第9节都是建模的内容. 第10节和第13节是 ...
- 之前在不网站看到过关于css的一些例子 今天自己也写了一个css特效
下面是代码 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <tit ...
- 安装visual_Paradigm,时序图的应用
此安装包已经上传到sunny的百度网盘. 删除,即,右击别的地方,然后,选择delete即可. 拖箭头,拖到某个实体上,再松开,会自动连线. 很好的一款画图工具.