[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,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
随机推荐
- 20155310 2016-2017-2 《Java程序设计》第三周学习总结
20155310 2016-2017-2 <Java程序设计>第三周学习总结 教材学习内容总结 1.对象(Object):存在的具体实体,具有明确的状态和行为. 2.类(Class):具有 ...
- 20155332 如何获取新技能+c语言学习调查
如何获取新技能+c语言学习调查 你有什么技能比大多人(超过90%以上)更好? 如果问我有没有什么技能比大多数人,并且是90%的人好,我还真不敢说有,因为世界上有70亿人,要比63亿人做的好才行啊.我也 ...
- 20145226 《Java程序设计》第3周学习总结
教材学习内容总结 学习目标 区分基本类型与类类型 理解对象的生成与引用的关系 掌握String类和数组 理解封装的概念 掌握构造方法的定义 理解重载的概念 掌握static的应用 教材第四章内容总结 ...
- ubuntu装openVPN会遇到的问题
与Windows系统相比,Linux下安装OpenVPN的过程就显得有点曲折. 如果你使用的是Ubuntu系统,你可以直接使用命令sudo apt-get install -y openvpn来安装O ...
- BZOJ2039_employ人员雇佣_KEY
题目传送门 网络流,求最小割. 设tot为所有盈利的和,即所有人(不花钱)雇佣. 对于S->i建一条容量为c[i]的边,i->j建一条S[i][j]*2的边,之所以这样建是因为如果不选这个 ...
- 最简单的rman操作
连接: [root@oracle000 ‾]# su - oracle [oracle@oracle000 ‾]$ rman target / lines) Recovery Manager: Rel ...
- 基于 OpenCV 的人脸识别
基于 OpenCV 的人脸识别 一点背景知识 OpenCV 是一个开源的计算机视觉和机器学习库.它包含成千上万优化过的算法,为各种计算机视觉应用提供了一个通用工具包.根据这个项目的关于页面,OpenC ...
- Deep Learning 教程翻译
Deep Learning 教程翻译 非常激动地宣告,Stanford 教授 Andrew Ng 的 Deep Learning 教程,于今日,2013年4月8日,全部翻译成中文.这是中国屌丝军团,从 ...
- day2 RHCE
1.配置SELINUX 在system1和system2上要求SeLinux的状态为enforcing.要求系统重启后依然生效. server [root@server0 ~]# getenforce ...
- LVS入门篇(五)之LVS+Keepalived实战
一.实验架构和环境说明 (1)本次基于VMware Workstation搭建一个四台Linux(CentOS 7.4)系统所构成的一个服务器集群,其中两台负载均衡服务器(一台为主机,另一台为备机), ...