这道题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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. leetCode 70.Climbing Stairs (爬楼梯) 解题思路和方法

    Climbing Stairs  You are climbing a stair case. It takes n steps to reach to the top. Each time you ...

  4. 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 ...

  5. Leetcode#70. Climbing Stairs(爬楼梯)

    题目描述 假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数. 示例 1: 输入: 2 输出: 2 解 ...

  6. 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 ...

  7. [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 ...

  8. LeetCode 70 Climbing Stairs(爬楼梯)(动态规划)(*)

    翻译 你正在爬一个楼梯. 它须要n步才干究竟顶部. 每次你能够爬1步或者2两步. 那么你有多少种不同的方法爬到顶部呢? 原文 You are climbing a stair case. It tak ...

  9. 【leetcode】Climbing Stairs

    题目简述: You are climbing a stair case. It takes n steps to reach to the top. Each time you can either ...

随机推荐

  1. InnoDB FULLTEXT

    1.概要 InnoDB引擎对FULLTEXT索引的支持是MySQL5.6新引入的特性,之前只有MyISAM引擎支持FULLTEXT索引.对于FULLTEXT索引的内容可以使用MATCH()…AGAIN ...

  2. 如何快速实现一个command

    新建一个类,实现icoomand接口 定义一个委托,为测试方便,先不考虑CanExecute的情况. 越简单越好. 代码如下: public class ExitHandler : ICommand ...

  3. springboot取得resources下的文件

    参考http://blog.csdn.net/programmeryu/article/details/58002218 ResourceUtils.getFile("classpath:p ...

  4. 第七章 二叉搜索树(d4)AVL树:(3+4)-重构

  5. Drying

    Drying http://poj.org/problem?id=3104 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 2 ...

  6. centos7,Python2.7安装request包

    1.安装epel扩展源:“sudo yum install epel-release” 2.安装python-pip:“sudo yum install python-pip” 3.升级pip:“su ...

  7. Django的admin介绍

    我们看到我们创建一个默认的django的项目,他在project的urls有一个admin的url的路径 我们访问这个路径,他是一个登陆框,需要输入用户名和密码 我们就需要创建这个用户名和密码,如果你 ...

  8. python之信号量【Semaphore】

    # 互斥锁同时只允许一个线程更改数据,而Semaphore是同时允许一定数量的线程更改数据,比如 # 一个厕所有3个坑,那么最多只允许3个人上厕所,后面的人只能等里面有人出来了才能再进去 import ...

  9. 3.滑雪-深搜&dp

    //Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道载一个区域中最长底滑 ...

  10. git 常用命令笔记

    #提交代码会加上用户名和邮箱 git config --global user.name 名字 git config --global user.email 邮箱 git config --globa ...