[LeetCode] Climbing Sairs
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?
思路:这道题是斐波那契数列的延伸。首先用最简单的递归的方法
class Solution {
public:
int climbStairs(int n) {
if (n <= ) return n;
return climbStairs(n - ) + climbStairs(n - );
}
};
不出意料的超时了。替代递归的方法是用动态规划。
class Solution {
public:
int climbStairs(int n)
{
vector<int> res(n+);
res[] = ;
res[] = ;
for (int i = ; i <= n; i++)
{
res[i] = res[i-] + res[i-];
}
return res[n];
}
};
时间复杂度降低了,接下来降低空间复杂度。用变量代替数组
class Solution {
public:
int climbStairs(int n)
{
if (n <= ) return n;
int f1 = ;
int f2 = ;
int f3 = ;
for (int i = ; i <= n; ++i) {
f3 = f2 + f1;
f1 = f2;
f2 = f3;
}
return f3;
}
};
最终的时间复杂度O(n),空间复杂度O(1)
[LeetCode] Climbing Sairs的更多相关文章
- [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——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 (Sequence DP)
Climbing Stairs https://oj.leetcode.com/problems/climbing-stairs/ You are climbing a stair case. It ...
- LeetCode:Climbing Stairs(编程之美2.9-斐波那契数列)
题目链接 You are climbing a stair case. It takes n steps to reach to the top. Each time you can either c ...
- [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 斐波那契数列
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- leetcode Climbing Stairs python
class Solution(object): def climbStairs(self, n): """ :type n: int :rtype: int " ...
- LeetCode 解题报告索引
最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
随机推荐
- 20155214 2016-2017-2 《Java程序设计》第2周学习总结
20155214 2016-2017-2 <Java程序设计>第2周学习总结 教材学习内容总结 Java的基本类型比C多了boolean型和byte型,缺少了long double型,ch ...
- WPF 动画:同为控件不同命 - 简书
原文:WPF 动画:同为控件不同命 - 简书 1. 及格与优秀 读大学的时候,有一门课的作业是用 PPT 展示. 但是我们很多同学都把 PPT 当做 Word 来用,就单纯地往里面堆文字. 大家都单纯 ...
- 【转载】漫谈C++:良好的编程习惯与编程要点
原文: 漫谈C++:良好的编程习惯与编程要点 阅读目录 以良好的方式编写C++ class Class with pointer member(s):记得写Big Three static与类 正文 ...
- (三)Hololens Unity 开发之 语音识别
学习源于官方文档 Voice input in Unity 笔记一部分是直接翻译官方文档,部分各人理解不一致的和一些比较浅显的保留英文原文 (三)Hololens Unity 开发之 语音识别 Hol ...
- XAF-属性编辑器中的EditMask,DisplayFormat格式化字符串该如何设置
XAF项目中有个DisplayFormat和EditMask设置,其中: 任何地方看到的DisplayFormat都是用于显示时,即非修改状态的编辑器,显示值的格式. EditMask是指编辑时的格式 ...
- Windows隐藏账户
win7系统用户由于共享文件,会开启Guest来宾帐户,开启Guest来宾帐户后发现登录界面会显示guest帐户,但是只有在有密码的情况下才会显示,很多用户不喜欢显示guest帐户,那么Win7登录界 ...
- Migrating to WebSphere 9
Migrating to WebSphere 9 Make a migration plan Requirements Migrate WebSphere profiles into the new ...
- const与readonly常量
const与readonly常量 const与readonly都是用来定义常量,但是它们有什么区别呢? 下面我们来简要的说明一下: const修饰的常量是编译时常量,如:public const St ...
- .net core 2.1.3可能引发Could not load file or assembly XXXXX的错误
参考文档: https://github.com/aspnet/Home/issues/3503 写在前面 感觉自己现在干的活离开发越来越远了啊,不过也很好,每天能学到不少东西,中文的,英文的,永远也 ...
- 使用appcmd命令创建iis站点及应用程序池
参考文章:iis7 appcmd的基础命令及简单用法 验证环境:Windows 7 IIS7 AppCmd.exe工具所在目录 C:\windows\sytstem32\inetsrv\目录下, ...