这道题参考了这个网址: http://blog.csdn.net/u012490475/article/details/48845683

/*

首先考虑边界情况,当有1层时,有一种方法。 
然后再看2层时,有1+1、和2+0,两种方法。 
再看3层时,首先有两种选择:走一步或者走两步。 
如果走两步,那后面还剩一步可走; 
如果走一步,后面还剩两步可走,后面的方法即可等同于上面的2层情况。 
即可归纳出用C(i) = j; 表示n层时有j种可能。 
C(1) = 1; 
C(2) = 2; 
C(3) = C(3-2) + C(3-1); //因为只有两种选择. 
C(4) = C(4-2) + C(4-1); 
… 
C(n) = C(n-2) + C(n-1);

*/

 public int climbStairs(int n) {
int a = 1;
int b = 1;
int c = 1;
for(int i = 1; i < n; i++)
{
c = a + b;
a = b;
b = c;
}
return c;
}

上面的是iteration的做法,下面是递归,非常简洁。

 public static int climbStairs(int n) {
// write your code here
if (n == 1) return 1;
else if (n == 2) return 2;
else return climbStairs(n - 1) + climbStairs(n - 2);
}

另外看到Python的解法,更为简洁精妙

/*

经典的动态规划问题。每次上一个台阶或者两个台阶,问一共有多少种方法到楼顶。这个实际上就是斐波那契数列的求解。可以逆向来分析问题,如果有n个台阶,那么走完n个台阶的方式有f(n)种。而走完n个台阶有两种方法,先走完n-2个台阶,然后跨2个台阶;先走完n-1个台阶,然后跨1个台阶。所以f(n) = f(n-1) + f(n-2)。

*/

Ref: http://bookshadow.com/weblog/2015/08/23/leetcode-climbing-stairs/

LintCode 111 Climbing Stairs的更多相关文章

  1. 111. Climbing Stairs 【LintCode easy】

    Description You are climbing a stair case. It takes n steps to reach to the top. Each time you can e ...

  2. [LintCode] 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] Climbing Stairs 爬梯子问题

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...

  4. Leetcode: climbing stairs

    July 28, 2015 Problem statement: You are climbing a stair case. It takes n steps to reach to the top ...

  5. 54. Search a 2D Matrix && Climbing Stairs (Easy)

    Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This m ...

  6. Climbing Stairs

    Climbing Stairs https://leetcode.com/problems/climbing-stairs/ You are climbing a stair case. It tak ...

  7. 3月3日(6) Climbing Stairs

    原题 Climbing Stairs 求斐波那契数列的第N项,开始想用通项公式求解,其实一个O(n)就搞定了. class Solution { public: int climbStairs(int ...

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

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

  9. 【LeetCode练习题】Climbing Stairs

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

随机推荐

  1. mac--又发现了一款mac快捷键神器

    之前用applescript,还有keyboard maestro,前段时间用机械键盘的时候,用开源的karabiner重新设置机械键盘的键位. 今天因为网上抄的键位F4想换成Launchpad,所以 ...

  2. 3.使用git提交项目到开源中国(gitosc)

    1.提交地址 使用的是开源中国git仓库 git.oschina.net 在windos环境下使用msysgit. 2.初始化化 username.email初始化 git config --glob ...

  3. android创建桌面快捷方式(启动目标非项目的启动页)

    1.布局文件中,目标Activity加入以下filter <intent-filter>                  <action android:name="an ...

  4. ThinkPHP 3.2.3 文件上传时间目录问题

    上传文件的代码如下 在上传文件的时候会默认生成时间目录, 但是有些时候,并不想生成时间目录,而是储存在我们自己定义的目录下,可以这样做: 只需要添加 $upload->autoSub = fal ...

  5. Linux学习(一):从图形界面进入命令行及命令行进入图形界面

    一.从图形界面进入命令行 最近脑洞大开,想接触一下linux.本人设备是win7,于是安了VMware(12.0.0)虚拟机,在安Linux(我用的CentOS 6.3 64)时不太会搞,跟据提示(英 ...

  6. 怎样获取本机的ip地址

    首先介绍一下用到的结构体 struct hostent { const char *h_name; // official name of host char **h_aliases; // alia ...

  7. div的一些易出错地方

    1.div中放一张图片老是显示不出来? 解决方法如下: 设置一下div的宽度与高度,然而此时直接写width与height是不对的,对于块级元素没有这个属性,只能在style="width: ...

  8. .net mvc 扩展IPrincipal接口

    .自定义实现IPrincipal接口的类 interface ICustomPrincipal : IPrincipal { string Identifier { get; set; } strin ...

  9. 【Java】:googleSearch

    google custom search是一个基于google的搜索引擎api,可以请求谷歌的搜索数据 pala pala  pala  ... 实现: 1.注册谷歌账号 2.创建google项目 1 ...

  10. .Net 对App.config和Web.config的访问操作(增、删、读、改)

    一.首先引用Configuration 1)App.config如下: using System.Configuration;//若果还没有Configuration,右键引用文件夹添加引用,在.NE ...