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 ...
随机推荐
- 【LeetCode算法题库】Day1:TwoSums & Add Two Numbers & Longest Substring Without Repeating Characters
[Q1] Given an array of integers, return indices of the two numbers such that they add up to a specif ...
- beego跨域请求配置
不说废话 在main函数前加入如下代码 func init() { //跨域设置 var FilterGateWay = func(ctx *context.Context) {ctx.Respons ...
- Sqlmap常用命令大全
1 Options(选项) -h,--help 显示帮助消息-hh 显示详细帮助-version -v VERBOSE 详细级别 0-6 默认12 Target 目标-u URL--url=URL-g ...
- js备忘录6
- Arithmatic项目修改总结
Arithmatic项目修改总结 github仓库 arithmatic3.0 一. 修改介绍 修改后类图: 1.关于类的合并 ==可以发现相比右边(旧)的类,左边(新)的类少了很多,这是我这次大改的 ...
- Javascript实现大整数加法
记得之前面试还被问到过用两个字符串实现两个大整数相加,当时还特别好奇好好的整数相加,为什么要用字符串去执行.哈哈,感觉当时自己还是很无知的,面试官肯定特别的无奈.今天在刷算法的时候,无意中看到了为什么 ...
- 人and绩效and职业道德
人行走在这个世界上 避免不了的是各种悲哀 人就像是一个茶几 上面放满了各种杯具 而要做的是要么把杯具打碎了咽下去,要么被杯具打晕 本布衣 躬耕于南阳 不求闻达于诸侯 每个人都可以选择自己的生活方式 或 ...
- Software-Defined Networking A Comprehensive Survey(一)
传统网络:1 复杂,难于管理 2 很难实现根据之前定义的方案进行配置,3 对于缺陷.变化不能够再次进行配置 4 控制和数据平面绑定在一起,使许多缺陷难于解决 SDN网络:通过打破传统网络垂直整合,从底 ...
- Maya学习笔记
软件: Maya 2016 : 参考教材: Maya 2016 中文版标准教程 ; 改变视图颜色 [窗口]|[设置/首项选择]|[颜色设置]|[3D视图]: 观察视图 旋转视图 Alt + 鼠标左键 ...
- Java中DAO的实现
J2EE 开发人员使用数据访问对象(Data Access Object DAO)设计模式,以便将低级别的数据访问逻辑与高级别的业务逻辑分离.实现 DAO 模式涉及比编写数据访问代码更多的内容.在本文 ...