[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,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
随机推荐
- 20155231 2016-2017-2 《Java程序设计》第2周学习总结
20155231 2016-2017-2 <Java程序设计>第2周学习总结 教材学习内容总结 学习目标: 了解java编程风格 认识java的类型与变量 掌握java流程控制 第三章基础 ...
- 20155315实验四 Android程序设计
实验内容 安装 Android Stuidio 学习Android Stuidio调试应用程序 实验要求 1.没有Linux基础的同学建议先学习<Linux基础入门(新版)><Vim ...
- ruby学习笔记(2)-chomp,chop的区别
还没开始系统性的学习Ruby,最近在看metasploit框架的exploit会涉及到Ruby脚本,也就硬着头皮一遍查阅资料一遍做些笔记吧. Ruby字符串中存在chop和chomp的内置函数.我在h ...
- WPF RoutedEvent and HitTest - 简书
原文:WPF RoutedEvent and HitTest - 简书 学习的时候切忌心浮气躁,慢慢的过每一个知识点,不要漏掉任何细节.不然当遇到细节问题的时候,会恼,会闹,会悔不该当初--花一下午调 ...
- Android开发——支付宝和微信支付快速接入流程
一.Android快速实现支付宝支付 1.首先,我们需要前往支付宝开放平台,申请我们的支付功能:https://open.alipay.com/platform/home.htm 支付宝首页 这里 有 ...
- python 多线程笔记(1)-- 概念
本文对不使用线程和使用线程做了一个对比. 假设有两件事情:听歌.看电影 一.不用线程 import time songs = ['爱情买卖','朋友','回家过年','好日子'] movies = [ ...
- Dlib库中实现正脸人脸检测的测试代码
Dlib库中提供了正脸人脸检测的接口,这里参考dlib/examples/face_detection_ex.cpp中的代码,通过调用Dlib中的接口,实现正脸人脸检测的测试代码,测试代码如下: #i ...
- day2 Opencv + image
[参考网站]http://backyardlife.duapp.com/duan/ 1.目标: 读入一幅图像,怎样显示一幅图像,以及如何保存一幅图像 cv2.imread(),cv2.imshow() ...
- 【BZOJ4008】[HNOI2015]亚瑟王
[BZOJ4008][HNOI2015]亚瑟王 题面 bzoj 洛谷 题解 由期望的线性性 可以知道,把所有牌打出的概率乘上它的伤害加起来就是答案 记第$i$张牌打出的概率为$fp[i]$ 则 $$ ...
- pager-taglib分页注意事项
必须先导包,尤其是 jsp 这种工具类和标签库的