[leetcode] 14. Climbing Stairs
这道题leetcode上面写着是DP问题,问题是我一开始写了个简单的递归结果直接超时,所以没办法只好拿迭代来做了。题目如下:
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?
这个基本就可以很简单的做成递归。如果楼层是1,那么返回1;如果是2的话,返回2;如果是大于2的n,就可以分为两条线,一条是看作n-1的楼层和1层楼,一条是看作n-2的楼层和2层楼。写成递归就是F(n)=F(n-1) + F(n-2)。就是将这两条线路相加总数,从结果上看就是斐波那契数列。
从递归的角度来写就是这样
class Solution {
public:
int climbStairs(int n)
{
if (n == 1)
{
return 1;
}
if (n == 2)
{
return 2;
}
return climbStairs(n - 1) + climbStairs(n - 2);
}
};
当然这样的话时间消耗非常夸张,在输入44后足足等了快20秒。所以为了降低消耗只能写成了迭代,如下:
class Solution {
public:
int climbStairs(int n)
{
if (n == 1)
{
return 1;
}
if (n == 2)
{
return 2;
}
int aspros = 1;
int deferos = 2;
int star = 0;
for (int i = 0; i < n-2; i++)
{
star = aspros + deferos;
aspros = deferos;
deferos = star;
}
return star;
}
};
通过。
[leetcode] 14. Climbing Stairs的更多相关文章
- [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 ...
- leetCode 70.Climbing Stairs (爬楼梯) 解题思路和方法
Climbing Stairs You are climbing a stair case. It takes n steps to reach to the top. Each time you ...
- 42. leetcode 70. Climbing Stairs
70. Climbing Stairs You are climbing a stair case. It takes n steps to reach to the top. Each time y ...
- Leetcode#70. Climbing Stairs(爬楼梯)
题目描述 假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数. 示例 1: 输入: 2 输出: 2 解 ...
- LN : leetcode 70 Climbing Stairs
lc 70 Climbing Stairs 70 Climbing Stairs You are climbing a stair case. It takes n steps to reach to ...
- [LeetCode OJ]-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(爬楼梯)(动态规划)(*)
翻译 你正在爬一个楼梯. 它须要n步才干究竟顶部. 每次你能够爬1步或者2两步. 那么你有多少种不同的方法爬到顶部呢? 原文 You are climbing a stair case. It tak ...
- 【leetcode】Climbing Stairs
题目简述: You are climbing a stair case. It takes n steps to reach to the top. Each time you can either ...
随机推荐
- python -m
影响sys.path python xxx.py python -m xxx.py 这是两种加载py文件的方式:1叫做直接运行2把模块当作脚本来启动 直接启动是把脚本所在的目录放到了sys.path属 ...
- 问题解决Android studio遇到 java.lang.OutOfMemoryError: GC app:transformClassesWithDexForDebug解决方法 以及gradle优化
http://blog.csdn.net/xiaoxing0828/article/details/52242090
- Visual Studio 2008 简体中文正式版下载及序列号(无使用期限限制,正式版)
VS2008中文试用版刚出来不久就上网拖了下来可是安装的时候却出问题了.后来,Google了下,把自己遇到的问题和解决方法跟大家分享.1.让试用版变成正式版 从Microsoft那下了7个压缩文 ...
- django的流程分析
首先实现一个前端输入网址,后端返回不同的html文件的步骤 一.配置django 1.创建工程 2.创建app 二.配置setting 1.配置templates路径 2.注释跨站请求 三.配置pro ...
- php Pthread 多线程 (四) 共享内存
有些时候我们希望在多个线程中共享一些需要的数据,我们可以使用shmop扩展. <?php class Count extends Thread { private $name = ''; pub ...
- 在ecplise中创建一个maven工程
1.我们首先需要在Ecplise中配置maven环境,详情见我的博客:https://www.cnblogs.com/wyhluckdog/p/10277278.html 2.maven projec ...
- win7系统administrator用户提示没有管理员权限,造装驱动安装错误,软件无法使用
警告1909.无法创建快捷方式 最近使用windows 7 32位安装软件,好多都失败.出现以上类似错误. 解决方法:在系统盘(C:)右键属性“安全”选项卡--“编辑”,添加“Everyone”设置为 ...
- PAT 1035 插入与归并(25)(代码+思路+测试点分析)
1035 插入与归并(25 分) 根据维基百科的定义: 插入排序是迭代算法,逐一获得输入数据,逐步产生有序的输出序列.每步迭代中,算法从输入序列中取出一元素,将之插入有序序列中正确的位置.如此迭代直到 ...
- usaco oct09 Watering Hole
Farmer John希望把水源引入他的N (1 <= N <= 300) 个牧场,牧场的编号是1~N.他将水源引入某个牧场的方法有两个,一个是在牧场中打一口井,另一个是将这个牧场与另一个 ...
- ubuntu下常用操作
屏幕截图: 可以用ubuntu自带的截图软件:gnome-screenshot. 该工具截图区域并且复制到剪切板命令为 gnome-screenshot -c -a,可以给该命令添加快捷方式,alt ...