这道题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)。就是将这两条线路相加总数,从结果上看就是斐波那契数列。

从递归的角度来写就是这样

  1. class Solution {
  2. public:
  3. int climbStairs(int n)
  4. {
  5. if (n == 1)
  6. {
  7. return 1;
  8. }
  9.  
  10. if (n == 2)
  11. {
  12. return 2;
  13. }
  14.  
  15. return climbStairs(n - 1) + climbStairs(n - 2);
  16. }
  17. };

当然这样的话时间消耗非常夸张,在输入44后足足等了快20秒。所以为了降低消耗只能写成了迭代,如下:

  1. class Solution {
  2. public:
  3. int climbStairs(int n)
  4. {
  5. if (n == 1)
  6. {
  7. return 1;
  8. }
  9.  
  10. if (n == 2)
  11. {
  12. return 2;
  13. }
  14.  
  15. int aspros = 1;
  16. int deferos = 2;
  17. int star = 0;
  18.  
  19. for (int i = 0; i < n-2; i++)
  20. {
  21. star = aspros + deferos;
  22. aspros = deferos;
  23. deferos = star;
  24. }
  25.  
  26. return star;
  27. }
  28. };

通过。

[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. 奶牛易物小组 Alpha冲刺

    项目地址:https://gitee.com/rrycbar/NenuChange 1 第一天 日期:2018/6/14 1.1 今日完成任务情况以及遇到的问题. 吴建瑜: 完成任务: 1.继续完善购 ...

  2. Gviz

    1) Introduction 为了理解基因组数据,通常旨在在基因组浏览器中绘制这样的数据,以及各种基因组注释特征,例如基因或转录物模型,CpG岛,重复区域等.这些功能可以从ENSEMBL或UCSC等 ...

  3. R语言做条形图时候,离散变量和连续型变量的区别

    1)条形图 条形图或许是最常用图形,常用来展示分类(different categories on the x-axis)和数值(numeric values on the y-axis)之间的关系. ...

  4. Andriod Studio adb 安装应用

    原文链接:https://blog.csdn.net/u014608640/article/details/51833304 下面的命令安装.重新安装和卸载应用程序. 安装:adb -s HT9BYL ...

  5. CentOS 6.3安装配置supervisor进程管理工具

    1. Supervisor是一个C/S系统,它可以在类unix操作系统让用户来监视和控制后台服务进程的数量,一个很重要的功能就是监控服务器的主要后台进程,并在出现问题是自动重启. 2. 根据服务器上的 ...

  6. Saving Tang Monk II(bfs+优先队列)

    Saving Tang Monk II https://hihocoder.com/problemset/problem/1828 时间限制:1000ms 单点时限:1000ms 内存限制:256MB ...

  7. postman 使用 - 连接不到接口

  8. [leetcode]250. Count Univalue Subtrees统计节点值相同的子树

    Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all nodes of ...

  9. php中正则案例分析

    案例一如下: $regex='/[\s\S]*/'; $str='lemon'; $matches=array(); if(preg_match($regex,$str,$matches)){ var ...

  10. springboot的yaml基础语法与取值,配置类,配置文件加载优先级

    1.基本语法k:(空格)v:表示一对键值对(一个空格必须有):以空格的缩进来控制层级关系:只要是左对齐的一列数据,都是同一个层级的属性和值也是大小写敏感: server: port: 8081 pat ...