这道题参考了这个网址: 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. 解决Linux系统下程序找不到动态库的方法

    思路:一般来说,通过make命令已经将程序依赖的动态库编译出来了,通过make install命令已经将动态库安装到系统的某个路径下.找没找到动态库就看这个路径是否包含在系统默认搜索动态库的路径中,如 ...

  2. ui-router中的锚点问题(angular中的锚点问题)

    angular.module('anchorScrollExample', []) .controller('ScrollController', ['$scope', '$location', '$ ...

  3. 2016. last day in office

    外面黑了,水面上黑魆魆的看不清楚了. 明天请假了,2017年再见! 2017加油! 2017 English improving!

  4. wcf 由 http 更改为 https 返回404,没有终结点在侦听可以接受消息的

    首先wcf项目在使用http时是没问题的. WCF有http更改为https之后,返回 没有终结点在侦听可以接受消息 需要修改wcf服务端及客户端 服务端更改代码 <binding maxRec ...

  5. asp.net Word Document Open return null

  6. HandlerThread源码分析

    其实原本HandlerThread的分析不应该单独开一篇博客的,应该在讲消息机制的那一片中一起分析. 但当时忘记了,而且今天第一次用MarkDown写博客,有点上瘾,就再来一篇,权当滥竽充数过过手瘾. ...

  7. nginx+tomcat负载均衡

    最近练习nginx+tomcat负载均衡.根据一些资料整理了大体思路,最终实现了1个nginx+2个tomcat负载均衡. 安装JDK 1>进入安装目录,给所有用户添加可执行的权限 #chmod ...

  8. 史上最全的常用iOS的第三方框架

    文章来源:http://blog.csdn.net/sky_2016/article/details/45502921 图像: 1.图片浏览控件MWPhotoBrowser       实现了一个照片 ...

  9. 常用邮件服务器名(POP3,SMTP地址)

    POP3服务器地址:pop3.sina.com.cn(端口:110)SMTP服务器地址:smtp.sina.com.cn(端口:25) sina.cn: POP3服务器地址:pop3.sina.com ...

  10. 先学习下一些基础的js和xpath语法

    这两个方法到底是在做什么呢?其实就是克隆了当前指令的节点,并生成子作用域.克隆的节点由transclude定义,如果你的属性是true,则克隆的是指令模板中的ng-transclude所在的DOM节点 ...