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. VC使用#定义方便控制版本号的宏

    一个 VC Project 中,可能有很多地方需要用到版本号,比如 About 对话框.版本资源等.如果每次版本更改都一一去改变这些值,不但非常麻烦,而且有悖唯一原则. 巧妙地使用宏定义,可以很好地解 ...

  2. poj 1077-Eight(八数码+逆向bfs打表)

    The 15-puzzle has been around for over 100 years; even if you don't know it by that name, you've see ...

  3. LeeCode-Majority Element

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  4. shell脚本书写总结

    1.shell脚本,如果重定向到文件中,则在脚本中不可以sed -i,如果用了sed -i ,则自打用了sed -i之后的打印都不能再重定向到输出文件了. 2.shell脚本中,如果将命令写在字符串里 ...

  5. ACM学习-POJ-1125-Stockbroker Grapevine

    菜鸟学习ACM,纪录自己成长过程中的点滴. 学习的路上,与君共勉. ACM学习-POJ-1125-Stockbroker Grapevine Stockbroker Grapevine Time Li ...

  6. 跟我一起学extjs5(18--模块的新增、改动、删除操作)

    跟我一起学extjs5(18--模块的新增.改动.删除操作)         上节在Grid展示时做了一个金额单位能够手工选择的功能,假设你要增加其它功能.也仅仅要依照这个模式来操作即可了,比方说你想 ...

  7. AngularJS 的一些坑

    UI的闪烁 Angular的自动数据绑定功能是亮点,然而,他的另一面是:在Angular初始化之前,页面中可能会给用户呈现出没有解析的表达式.当DOM准备就绪,Angular计算并替换相应的值.这样就 ...

  8. TS相关知识点

    数字电视的TS包和TS流的组成和功能 综合考虑几下几个因素: (1)包的长度不能过短,否则包头开销所占比例过大, 导致传输效率下降 (2)包的长度不能过长,否则在丢失同步的情况下恢复同步的 周期过长, ...

  9. JS局部打印两种方法

    所有浏览器都可以 <html> <head title=""> <title>测试打印</title> <style medi ...

  10. java中String的用法

    String的用法很活跃,也用到的很多.可以根据自己的需要查询API.这里只有concat和substring,indexof的用法 class TestString { public static ...