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 ...
随机推荐
- NO.4:自学python之路------内置方法、装饰器、迭代器
引言 是时候开始新的Python学习了,最近要考英语,可能不会周更,但是尽量吧. 正文 内置方法 Python提供给了使用者很多内置方法,可以便于编程使用.这里就来挑选其中大部分的内置方法进行解释其用 ...
- 笨办法学Python - 习题11-12: Asking Questions & Prompting People
目录 1.习题 11: 提问 2.习题 12: 提示别人 3.总结 1.习题 11: 提问 学习目标:了解人机交互场景,熟悉raw_input 的用法. 1.在 Python2.x 中 raw_inp ...
- Daily Scrum (2015/10/29)
今天晚上我们学霸项目的三个小组在一起开会,讨论如何能在后期使我们三个项目更好地结合在一起.为了三个小组的能够同时工作,不出现某一小组因需要其他小组成果而停滞的情况,我们决定围绕lucene,solr, ...
- [2019BUAA软件工程]第0次个人作业
我 & 计算机 写在前面 撰写本博客时,笔者正就读北航计算机系大三下的软件工程课程.借由这次博客作业的机会,笔者从高考时与计算机专业结缘.大学对计算机的学习以及对未来的计划三方面进行了些许 ...
- Hibernate笔记②--hibernate类生成表、id生成策略、级联设置、继承映射
一.多表的一个关联关系 老师和学生是一对多的关系 student:tid属性 外键约束 对应teacher表中的id属性 teacher:id 在myeclipse的db窗口中选中两个表来生成类. ...
- Java编程题每日一练day1
Day1共7题 package com.pcx.day1; /** * 设一个字符数组,对其元音字母进行统计 * a e i o u * @author Administrator * */ publ ...
- [Elite 2008 Dec USACO]Jigsaw Puzzles
#include <iostream> #include <cstdio> #include <cstring> using namespace std; #def ...
- Alpha版本冲刺(三)
目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:凯琳 组员6:丹丹 组员7:家伟 组员8:政演 组员9:鸿杰 组员10:刘一好 组员11:何宇恒 展示组内最 ...
- Java面试&编写程序:使子线程循环10次,紧接着主线程循环100次,来回50次
package com.cwcec.test; public class TraditionalThreadCommunication { /** * @param args */ public st ...
- Python入门:字符串的分片与索引、字符串的方法
这是关于Python的第3篇文章,主要介绍下字符串的分片与索引.字符串的方法. 字符串的分片与索引: 字符串可以用过string[X]来分片与索引.分片,简言之,就是从字符串总拿出一部分,储存在另一个 ...