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. cf472B Design Tutorial: Learn from Life

    B. Design Tutorial: Learn from Life time limit per test 1 second memory limit per test 256 megabytes ...

  2. UESTC_One Step Two Steps CDOJ 1027

    As we all know,the handsome boy, Zcat, has a fever of flat shoes. He sings it on the class, in the d ...

  3. error LNK2019

    error LNK2019: 无法解析的外部符号 "public: virtual __thiscall Fruit::~Fruit(void)" (??1Fruit@@UAE@X ...

  4. Java中基础类库使用

    Java中基础类库: 在这里我仅仅介绍几种我个人觉得会常常使用的 1:Object类中的Clone机制仅仅是对对象进行浅层次的克隆,假设须要进行深层次的克隆的话那么就要自己写(详细Clone方法请參考 ...

  5. 【SQL】大杂烩

    --------------------------------- 索引 --------------------------------- 语法: CREATE [索引类型] INDEX 索引名称 ...

  6. iframe 重新加载闪过白块问题

    在使用iframe时,iframe背景为白块,刷新时也会闪过白块.如果刷新时间长,就会一直出现白块,让人很烦恼,通过网上搜资料,测试最终解决方法如下所示,注意主要针对IE浏览器测试. 一.iframe ...

  7. Sql Server 2012启动存储过程

    可以通过如下步骤创建 1.打开show advanced options reconfigure 2.打开scan for startup procs,使得sql server在启动时扫描需要运行的p ...

  8. 000-C#基础

    C#中数据类型的继承关系如下 System.Object |-------------System.ValueType | |-------System.Boolean | |-------Syste ...

  9. USACO 1.3... 虫洞 解题报告(搜索+强大剪枝+模拟)

    这题可真是又让我找到了八数码的感觉...哈哈. 首先,第一次见题,没有思路,第二次看题,感觉是搜索,就这样写下来了. 这题我几乎是一个点一个点改对的(至于为什么是这样,后面给你看一个神奇的东西),让我 ...

  10. isNUll ,对于sql中的一个表的description的NULL和空格的处理

    select client.clientname ,description,'client2'= case when client.Description IS NULL then  client.c ...