[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,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
随机推荐
- python append extend区别
1. 列表可包含任何数据类型的元素,单个列表中的元素无须全为同一类型. 2. append() 方法向列表的尾部添加一个新的元素. 3. 列表是以类的形式实现的.“创建”列表实际上是将一个类实例化.因 ...
- day1 post验证登录
用post方式模拟 1.登录抽屉网 2.登录代码 ,URL,Form Data 中的信息写入 # coding=utf-8 #post 登录验证 import requests form_data = ...
- 【LG3238】 [HNOI2014]道路堵塞
题目描述 给你一张\(N\)个点.\(M\)条边的有向图,按顺序给定你一条有\(L\)条边的\(1\rightarrow n\)的最短路, 每次断掉这\(L\)条边中的一条(不对后面答案产生影响),求 ...
- Zabbix学习之路(五)之MySQL监控
1.linux-node2节点安装数据库 [root@linux-node2 ~]# yum install -y mariadb-server [root@linux-node2 ~]# syste ...
- 试用一下markdown
1 2 3 4 5 6 Blog
- 查询数据库所有表和字段及其注释(mysql)
#查询某个库所有表 select * from information_schema.TABLES where table_schema = '数据库' #查询某个库所有表的字段 select * f ...
- 关于ExecuteNonQuery执行的返回值(SQL语句、存储过程)
因为msdn中说返回受影响的行数: Executes a Transact-SQL statement against the connection and returns the number of ...
- VBA_常用VBA代码
'批量替换字符 Sub Test() Dim i As Integer ).Value = "已激活" Then Cells(i, ).Value = "Active&q ...
- python-编程从入门到实践
python-编程从入门到实践 1.python文件后缀名: .py 是Python的源码文件,由Python.exe解释. .pyc 是Python的编译文件.pyc 文件往往代替 py 文件发布: ...
- TCP/IP三次握手四次挥手分析
流程图 全部11种状态 客户端独有的:(1)SYN_SENT (2)FIN_WAIT1 (3)FIN_WAIT2 (4)CLOSING (5)TIME_WAIT 服务器独有的:(1)LISTEN (2 ...