LintCode 111 Climbing Stairs
这道题参考了这个网址: 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的更多相关文章
- 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 ...
- [LintCode] Climbing Stairs 爬梯子问题
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- [LeetCode] Climbing Stairs 爬梯子问题
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- Leetcode: climbing stairs
July 28, 2015 Problem statement: You are climbing a stair case. It takes n steps to reach to the top ...
- 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 ...
- Climbing Stairs
Climbing Stairs https://leetcode.com/problems/climbing-stairs/ You are climbing a stair case. It tak ...
- 3月3日(6) Climbing Stairs
原题 Climbing Stairs 求斐波那契数列的第N项,开始想用通项公式求解,其实一个O(n)就搞定了. class Solution { public: int climbStairs(int ...
- leetCode 70.Climbing Stairs (爬楼梯) 解题思路和方法
Climbing Stairs You are climbing a stair case. It takes n steps to reach to the top. Each time you ...
- 【LeetCode练习题】Climbing Stairs
Climbing Stairs You are climbing a stair case. It takes n steps to reach to the top. Each time you c ...
随机推荐
- python中的时间转换
Python中的时间相关库有: datetime 和time. 主要形式有: datetime timestamp 相互转换: timestamp->datetime: dt = datetim ...
- MVC后台数据赋值给前端JS对象
Controller中的数据,不管是使用的是ViewModel 还是ViewBag.Data,要将他传递到View中,这个很容易,但是如果要将它传递给JS中的某个对象,这个改如何处理呢? 后台的数据格 ...
- Spark RDD aggregateByKey
aggregateByKey 这个RDD有点繁琐,整理一下使用示例,供参考 直接上代码 import org.apache.spark.rdd.RDD import org.apache.spark. ...
- 07-JAVA继承与接口
动手动脑: 源程序: public class FuGai { public static void main(String[] args) { // TODO Auto-generated meth ...
- (转)如何在一台电脑上开启多个tomcat 和配置让系统识别哪个具体的tomcat
大家基本上都只在一台电脑上面启动一个Tomcat,而启动多个Tomcat会提示报错等相关故障.而假如调试负载均衡及集群的时候,需要在一台电脑上面开启多个Tomcat,那么怎么开启呢? 方法/步骤 首先 ...
- 构造 & 析构 & 匿名对象
以前仅知道创建对象,但对匿名对象的了解基本为0. 通过阅读google chromium源代码 中关于 log 的使用,查阅相关资料,了解了一下匿名对象,予以记录. 什么是匿名对象 匿名对象可以理 ...
- jfreechart 整合sturts2牛刀小试
一.增加的jar包 struts2-jfreechart-plugin-2.1.6.jar 在struts2的相应jar包中找 jcommon-1.0.23.jar ...
- 开始研究tigase和android客户端的实现
2015.7.5. 昨晚总算把tigaase7.0的开发环境搭起来了.稍微看了下代码结构. 主要是auth,db,compnent,cluster,server,xmpp,这几块,准备先发点时间看看开 ...
- Android框架之AndroidAnnotations详细讲解
一: (1)一个activity如过使用AndroidAnnotions注入时, 那么它在 AndroidManifest.xml注册时,应该加入_ 比如: MainActivity的注册时 < ...
- .NET entityframework for mysql ,datetime字段存储值时有误差
昨天Tester发现数据有问题,大部分时间“datetime类型”都多了一秒,很少一部分数据的时间能完全对上(年月日时分秒),因为缺少关键日志,就各种排查,最后发现在调用Savechange方法前一刻 ...