Climbing Stairs - LeetCode
题目链接
注意点
- 注意边界条件
解法
解法一:这道题是一题非常经典的DP题(拥有非常明显的重叠子结构)。爬到n阶台阶有两种方法:1. 从n-1阶爬上 2. 从n-2阶爬上。很容易得出递推式:f(n) = f(n-1)+f(n-2)于是可以得到下面这种最简单效率也最低的解法 —— 递归。
class Solution {
public:
int climbStairs(int n) {
if(n == 0 || n == 1 || n == 2)
{
return n;
}
return climbStairs(n-1)+climbStairs(n-2);
}
};

解法二:思路不变,改为更高效的写法 —— 迭代。时间复杂度O(n)。
class Solution {
public:
int climbStairs(int n) {
vector<int> ans;
int i;
for(i = 0;i <= 2;i++)
{
ans.push_back(i);
}
for(i = 3;i <= n;i++)
{
ans.push_back(ans[i-1]+ans[i-2]);
}
return ans[n];
}
};

解法三:继续优化,可以看出解法二中需要开一个额外的数组来保存过程中计算的值,这些值计算完之后就没用了,所以改用两个变量来替代。时间复杂度O(n),空间复杂度O(1)
class Solution {
public:
int climbStairs(int n) {
if(n == 0||n == 1||n == 2)
{
return n;
}
int a = 2,b = 1,i;
for(i = 0;i < n-2;i++)
{
a = a+b;
b = a-b;
}
return a;
}
};
或者一个更好理解的
class Solution {
public:
int climbStairs(int n) {
if(n == 0||n == 1||n == 2)
{
return n;
}
int a = 2,b = 1,ret,i;
for(i = 0;i < n-2;i++)
{
ret = a+b;
b = a;
a = ret;
}
return ret;
}
};

小结
- 这道题可以扩展到每次可以走k步,那递推式就变为
f(n) = f(n-1) + f(n-2) + ... + f(n-k)
Climbing Stairs - LeetCode的更多相关文章
- Min Cost Climbing Stairs - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Min Cost Climbing Stairs - LeetCode 注意点 注意边界条件 解法 解法一:这道题也是一道dp题.dp[i]表示爬到第i层 ...
- climbing stairs leetcode java
问题描述: You are climbing a stair case. It takes n steps to reach to the top. Each time you can either ...
- [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: climbing stairs
July 28, 2015 Problem statement: You are climbing a stair case. It takes n steps to reach to the top ...
- [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] Climbing Stairs (Sequence DP)
Climbing Stairs https://oj.leetcode.com/problems/climbing-stairs/ You are climbing a stair case. It ...
- [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] 70. Climbing Stairs 爬楼梯
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
随机推荐
- 《杜增强讲Unity之Tanks坦克大战》6-发射子弹
6 发射子弹 本节完成发射子弹的功能,最终代码如下: image 首先,发射子弹得确定发射的位置和方向,还有发射的初始速度.具体的发射速度和按下发射按键的时间长短有关,这个关于子弹的蓄力我们在第九 ...
- 解决Ubuntu“下载额外数据文件失败 ttf-mscorefonts-installer”的问题 (转载)
解决Ubuntu“下载额外数据文件失败 ttf-mscorefonts-installer”的问题 发表于 2017-09-15 | 更新于 2018-04-29 | 分类于 Linux | 评论数: ...
- Redis的C语言客户端(hiredis)的安装和使用
关键词:hiredis, cRedis, redis clients, redis客户端, C客户端, 华为云分布式缓存服务 hiredis是一个非常全面的C语言版redis接口库,支持所有命令.管道 ...
- 吴恩达 Deep learning 第二周 神经网络基础
逻辑回归代价函数(损失函数)的几个求导特性 1.对于sigmoid函数 2.对于以下函数 3.线性回归与逻辑回归的神经网络图表示 利用Numpy向量化运算与for循环运算的显著差距 import nu ...
- 基于Linux-3.9.4内核增加简单的时间片轮转功能
简单的时间片轮转多道程序内核代码 原创作品转载请注明出处https://github.com/mengning/linuxkernel/ 作者:sa18225465 一.安装 Linux-3.9.4 ...
- 快速删除docker中的容器
http://blog.csdn.net/cmzsteven/article/details/49230363
- java微信开发之接口连接
个人学习注册订阅号就行,把资料尽可能完善,不然开发时权限不够,然后登陆,点击订阅号然后扫码就可以微信关注.
- 将通过<input type="file">上传的txt文件存储在localStorage,提取并构建File对象
参考博文: JS 之Blob 对象类型 在本地存储localStorage中保存图片和文件 <input type="file" id="jobData" ...
- Thinking in Java & 内部类
静态内部类可以直接创建对象new B.C(); 如果内部类不是静态的,那就得这样 B b = new B(); B.C c = b.new C(); //要想直接创建内部类的对象,不能按照想象的方式, ...
- keil C 51 strlen库函数使用
在keil c51 程序中,若定义数组 volatile unsigned char data[3]={'G','G','G'};使用strlen(&data);得到的长度是不对的,若定义v ...