[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 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] 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爬楼梯 (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 ...
- Leetcode 70. Climbing Stairs 爬楼梯 (递归,记忆化,动态规划)
题目描述 要爬N阶楼梯,每次你可以走一阶或者两阶,问到N阶有多少种走法 测试样例 Input: 2 Output: 2 Explanation: 到第二阶有2种走法 1. 1 步 + 1 步 2. 2 ...
- 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 ...
随机推荐
- javascript判断mp3是否播放完
javascript判断mp3是否播放完 var audio=document.getElementById('audio'); if(audio){ audio.loop = false; audi ...
- Springboot创建项目(idea版本)
一:概述 由于springboot项目,不管是java工程还是web工程都可以直接以jar方式运行,所以推荐创建jar工程,这里创建jar工程项目为例. 二:两种方式创建springboot项目 1. ...
- IIS_CVE-2017-7269 IIS6.0远程代码执行漏洞复现
CVE-2017-7269 IIS6.0远程代码执行漏洞复现 一.漏洞描述 IIS 6.0默认不开启WebDAV,一旦开启了WebDAV,安装了IIS6.0的服务器将可能受到该漏洞的威胁. 二.影响版 ...
- Oracle查询日期字段是否包含时分秒 TRUNC() 函数
可以使用 ORACLE TRUNC()函数 来进行判断 表 A 日期字段 datetime 部分数据带时分秒,部分数据没有时分秒 select * from A where datetime = TR ...
- 跨域问题 Blocked a frame with origin "http://......" from accessing a cross-origin frame.
为了轻松偷懒,不想从目的项目中开发目标项目中的页面,但目的项目中需要获取老项目中的页面,这里用了iframe跨域链接页面出现了问题 Blocked a frame with origin " ...
- windows7系统 执行应用程序报 Error accessing specified device (Error: 2)
--------------------------- ---------------------------Error accessing specified device (Error: 2) - ...
- SAP 同一个序列号可以同时出现在2个不同的HU里?
SAP 同一个序列号可以同时出现在2个不同的HU里? 答案是可以的. 如下图示,HU 180141003288里的序列号11810010540121, 而序列号11810010540121已经出现在另 ...
- 9.为什么要进行系统拆分?如何进行系统拆分?拆分后不用 dubbo 可以吗?
作者:中华石杉 面试题 为什么要进行系统拆分?如何进行系统拆分?拆分后不用 dubbo 可以吗? 面试官心理分析 从这个问题开始就进行分布式系统环节了,现在出去面试分布式都成标配了,没有哪个公司不问问 ...
- linux下c语言实现多线程文件复制【转】
转自:https://www.cnblogs.com/zxl0715/articles/5365989.html .具体思路 把一个文件分成N份,分别用N个线程copy, 每个线程只读取指定长度字节大 ...
- 常用linux系统监视软件
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo ##epel源 yum install -y ...