[LeetCode] Climbing Stairs 爬梯子问题
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?
Note: Given n will be a positive integer.
Example 1:
- Input: 2
- Output: 2
- Explanation: There are two ways to climb to the top.
- 1. 1 step + 1 step
- 2. 2 steps
Example 2:
- Input: 3
- Output: 3
- Explanation: There are three ways to climb to the top.
- 1. 1 step + 1 step + 1 step
- 2. 1 step + 2 steps
- 3. 2 steps + 1 step
这篇博客最开始名字叫做爬梯子问题,总是有童鞋向博主反映移动端打不开这篇博客,博主觉得非常奇怪,自己也试了一下,果然打不开。心想着是不是这个博客本身有问题,于是想再开一个相同的帖子,结果还是打不开,真是见了鬼了。于是博主换了个名字,结果居然打开了?!进经过排查后发现,原来是“爬梯子”这三个字是敏感词,放到标题里面,博客就被屏蔽了,我也真是醉了,完全是躺枪好么,无奈之下,只好改名为爬楼梯问题了 -。-|||。
这个爬梯子问题最开始看的时候没搞懂是让干啥的,后来看了别人的分析后,才知道实际上跟斐波那契数列非常相似,假设梯子有n层,那么如何爬到第n层呢,因为每次只能爬1或2步,那么爬到第n层的方法要么是从第 n-1 层一步上来的,要不就是从 n-2 层2步上来的,所以递推公式非常容易的就得出了:dp[n] = dp[n-1] + dp[n-2]。 由于斐波那契额数列的求解可以用递归,所以博主最先尝试了递归,拿到 OJ 上运行,显示 Time Limit Exceeded,就是说运行时间超了,因为递归计算了很多分支,效率很低,这里需要用动态规划 (Dynamic Programming) 来提高效率,代码如下:
C++ 解法一:
- class Solution {
- public:
- int climbStairs(int n) {
- if (n <= ) return ;
- vector<int> dp(n);
- dp[] = ; dp[] = ;
- for (int i = ; i < n; ++i) {
- dp[i] = dp[i - ] + dp[i - ];
- }
- return dp.back();
- }
- };
Java 解法一:
- public class Solution {
- public int climbStairs(int n) {
- if (n <= 1) return 1;
- int[] dp = new int[n];
- dp[0] = 1; dp[1] = 2;
- for (int i = 2; i < n; ++i) {
- dp[i] = dp[i - 1] + dp[i - 2];
- }
- return dp[n - 1];
- }
- }
我们可以对空间进行进一步优化,只用两个整型变量a和b来存储过程值,首先将 a+b 的值赋给b,然后a赋值为原来的b,所以应该赋值为 b-a 即可。这样就模拟了上面累加的过程,而不用存储所有的值,参见代码如下:
C++ 解法二:
- class Solution {
- public:
- int climbStairs(int n) {
- int a = , b = ;
- while (n--) {
- b += a;
- a = b - a;
- }
- return a;
- }
- };
Java 解法二:
- public class Solution {
- public int climbStairs(int n) {
- int a = 1, b = 1;
- while (n-- > 0) {
- b += a;
- a = b - a;
- }
- return a;
- }
- }
C++ 解法三:
- class Solution {
- public:
- int climbStairs(int n) {
- vector<int> memo(n + );
- return helper(n, memo);
- }
- int helper(int n, vector<int>& memo) {
- if (n <= ) return ;
- if (memo[n] > ) return memo[n];
- return memo[n] = helper(n - , memo) + helper(n - , memo);
- }
- };
Java 解法三:
- public class Solution {
- public int climbStairs(int n) {
- int[] memo = new int[n + 1];
- return helper(n, memo);
- }
- public int helper(int n, int[] memo) {
- if (n <= 1) return 1;
- if (memo[n] > 0) return memo[n];
- return memo[n] = helper(n - 1, memo) + helper(n - 2, memo);
- }
- }
C++ 解法四:
- class Solution {
- public:
- int climbStairs(int n) {
- if(n <= ) return ;
- return climbStairs(n / ) * climbStairs(n - n / ) + climbStairs(n / - ) * climbStairs(n - n / - );
- }
- };
Java 解法四:
- public class Solution {
- public int climbStairs(int n) {
- if(n <= 1) return 1;
- return climbStairs(n / 2) * climbStairs(n - n / 2) + climbStairs(n / 2 - 1) * climbStairs(n - n / 2 - 1);
- }
- }
最后来看一种叼炸天的方法,其实斐波那契数列是可以求出通项公式的,推理的过程请参见 知乎上的这个贴子,那么有了通项公式后,直接在常数级的时间复杂度范围内就可以求出结果了,参见代码如下:
C++ 解法五:
- class Solution {
- public:
- int climbStairs(int n) {
- double root5 = sqrt();
- return ( / root5) * (pow(( + root5) / , n + ) - pow(( - root5) / , n + ));
- }
- };
Java 解法五:
- public class Solution {
- public int climbStairs(int n) {
- double root5 = Math.sqrt(5);
- double res = (1 / root5) * (Math.pow((1 + root5) / 2, n + 1) - Math.pow((1 - root5) / 2, n + 1));
- return (int)res;
- }
- }
Github 同步地址:
类似题目:
参考资料:
https://leetcode.com/problems/climbing-stairs/
https://leetcode.com/problems/climbing-stairs/discuss/25345/Easy-solutions-for-suggestions.
https://leetcode.com/problems/climbing-stairs/discuss/25296/3-4-short-lines-in-every-language
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Climbing Stairs 爬梯子问题的更多相关文章
- [LintCode] Climbing Stairs 爬梯子问题
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- [Leetcode] 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] Min Cost Climbing Stairs 爬楼梯的最小损失
On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once you pay ...
- [LeetCode] 746. Min Cost Climbing Stairs 爬楼梯的最小损失
On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once you pay ...
- [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: climbing stairs
July 28, 2015 Problem statement: You are climbing a stair case. It takes n steps to reach to the top ...
- [LeetCode] Climbing Stairs (Sequence DP)
Climbing Stairs https://oj.leetcode.com/problems/climbing-stairs/ You are climbing a stair case. It ...
- [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 ...
随机推荐
- 如何在Windows Server 2008 R2没有磁盘清理工具的情况下使用系统提供的磁盘清理工具
今天,刚好碰到服务器C盘空间满的情况,首先处理了临时文件和有关的日志文件后空间还是不够用,我知道清理C盘的方法有很多,但今天只分享一下如何在Windows Server 2008 R2没有磁盘清理工具 ...
- scikit-learn一般实例之七:使用多输出评估器进行人脸完成
本例将展示使用多输出评估期来实现图像完成.目标是根据给出的上半部分人脸预测人脸的下半部分. 第一列展示的是真实的人脸,接下来的列分别展示了随机森林,K近邻,线性回归和岭回归对人脸下半部分的预测. # ...
- 『.NET Core CLI工具文档』(八)dotnet-restore
说明:本文是个人翻译文章,由于个人水平有限,有不对的地方请大家帮忙更正. 原文:dotnet-restore 翻译:dotnet-restore 名称 dotnet-restore - 还原一个项目的 ...
- ASP.NET MVC实现权限控制
这篇分享一下 ASP.NET MVC权限控制.也就是说某一用户登录之后,某一个用户是否有权限访问Controller,Action(操作),视图等 想实现这些功能,需要在数据库创建好几个表:[User ...
- C#开发微信门户及应用(35)--微信支付之企业付款封装操作
在前面几篇随笔,都是介绍微信支付及红包相关的内容,其实支付部分的内容还有很多,例如企业付款.公众号支付或刷卡支付.摇一摇红包.代金券等方面的内容,这些都是微信接口支持的内容,本篇继续微信支付这一主题, ...
- Java终止线程
Thread提供了stop()方法终止线程,但是该方法是强行终止,容易产生一些错误,已经被废弃. 可以使用退出标志来终止线程,在run()函数里面设置while循环,把退出标志作为while的条件,当 ...
- Windows下快速安装Flask的一次经历
前提: 1.已安装python版本(一般都是2.X) 2.已安装easy_install python安装,记得配置Python的环境变量,例如:我的直接在Path上加 G:\Python 验证安装P ...
- 手把手教你用FineBI做数据可视化
前些日子公司引进了帆软商业智能FineBI,在接受了简单的培训后,发现这款商业智能软件用作可视分析只用一个词形容的话,那就是“轻盈灵动”!界面简洁.操作流畅,几个步骤就可以创建分析,获得想要的效果.此 ...
- Android MVP 设计模式
1.基本概念 Model : 模型 负责处理数据的加载或者存储,比如从网络或本地数据库获取数据等: View : 视图 负责界面数据的展示,与用户进行交互: Presenter ...
- 开启基本数据结构和算法之路--初识Graphviz
在我的Linux刀耕开荒阶段,就想开始重拾C,利用C实现常用的基本数据结构和算法,而数据结构和算法的掌握的熟练程度正是程序的初学者与职业程序员的分水岭. 那么怎么开启这一段历程呢? 按照软件工程的思想 ...