Climbing Stairs

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?

题目意思:

上楼梯。假设要n步到达楼梯的顶部。每一次你只能上一个或两级台阶,问要到达顶部一共有多少种方法?

解题思路:

真是太巧了!!我今天刚刚在《剑指offer》里读到了一模一样的原题,在该书的75页。简单介绍一下吧:

如果只有一级台阶,那显然只有一种方法。如果有2级台阶,那就有两种上的方法了:一种是分两次,每次上一级;另外一种就是一次上2级。

接着讨论一般情况,我们把n级台阶时的方法数看成是n的函数,记为f(n)。当n>2时,第一次上的时候就有两种不同的选择:一是第一次只登一级,此时登法数目等于后面剩下的n-1级台阶的登法数目,即f(n-1);另外一种选择是第一次上2级,此时方法数目等于后面剩下的n-2级台阶的方法数目,即f(n-2)。因此n级台阶的不同登法总数等于f(n-1)+f(n-2)。不难看出这实际上就是斐波那契数列了。

注:因为斐波那契解法大都采用递归,实际上递归的解法存在很严重的效率问题,有大量的重复计算。当要计算的数很大时,速度极慢且有可能出现函数调用栈溢出的情况。所以实用的解法是采用循环,时间复杂度是O(n)。

代码如下:

 class Solution {
public:
int climbStairs(int n) {
return Fibonacci(n);
} int Fibonacci(int n){
int result[] = {,};
if(n < ){
return result[n-];
} int NminusOne = ;
int NminusTwo = ;
int N = ;
for(int i = ; i <= n; i++){
N = NminusOne + NminusTwo; NminusTwo = NminusOne;
NminusOne = N;
}
return N;
}
};

【LeetCode练习题】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] 14. Climbing Stairs

    这道题leetcode上面写着是DP问题,问题是我一开始写了个简单的递归结果直接超时,所以没办法只好拿迭代来做了.题目如下: You are climbing a stair case. It tak ...

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

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

  10. 【leetcode】Climbing Stairs

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

随机推荐

  1. Silverlight 结合ArcGis 在地图画面上显示名称+ 点选图层事件委派

    原文 http://www.dotblogs.com.tw/justforgood/archive/2012/05/10/72083.aspx 如下图,我希望我的滑鼠经过此标记的点时显示名称 其实简单 ...

  2. ubuntu下编译源码级QT

    注意必须好好看官方文档: http://qt-project.org/doc/qt-5/linux.html 包括编译Qt库依赖的包等等. 编译过程中发现以下错误: All the OpenGL fu ...

  3. redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool

    超时 Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: jav ...

  4. SDL显示内存中的图像

    <textarea readonly="readonly" name="code" class="c++"> #include ...

  5. invesments 第三章 上

    1. How firms issue securities: 公司如何发行股票 A.       primary market: 新的股票,债券和其他的证券第一次发行的market B.        ...

  6. Path Sum 解答

    Question Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that addi ...

  7. 在Linux下如何创建LVM及LVM创建过程

    Linux LVM创建过程:(我用的是Centos6.8) 一.准备工作: LVM可以的创建可以在系统安装的过程中创建,也可以在安装完系统之后再创建建,都是可以的,我的是在系统安装完之后创建的,具体的 ...

  8. 使用 apache ant 轻松实现文件压缩/解压缩(转)

    原文地址:http://blog.csdn.net/irvine007/article/details/6779492 maven配置ant包: <dependency> <grou ...

  9. 只允许指定IP访问指定端口 ufw

    那天工作需要在服务器上指定ip才可以访问指定端口,配置命令如下: (服务器是ubuntu 14.04系统) apt-get install ufw ufw enable ufw default den ...

  10. 评教数据整理专用VBA小程序

    这次评教的所有数据存放在两个数据库中,比如说给某教师评论的学生有100个,可是结果有40个的数据在数据库A中,另外60人的数据在数据库B中.那么,如何将两个库中的数据整合,最后得到教师的准确成绩成为了 ...