我敢保证这道题是在今早蹲厕所的时候突然冒出的解法。第一次接触DP题,我好伟大啊啊啊~

题目:一个N阶的梯子,一次能够走1步或者2步,问有多少种走法。

解法:原始DP问题。

思路:

1、if N == 1 , then ans = 1;

2、if N == 2 , then ans = 2;

3、if 我们现在在N-1阶处,现在已经有f(N-1)种走法,那么到N阶处,则还是f(N - 1)种走法(再走一步到终点)。

4、if 我们现在在N-2阶处,现在已经有f(N-2)种走法,那么到N阶处,则还是f(N - 2)种走法(一下子走两步到终点;如果走一步到N-1阶处,这种走法已经包含在f(N-1)中了,因此从N-2阶处到N阶处只有f(N-2)种走法)

综上所述:f(N) = f(N-1) + f(N-2)

代码:

1、标准回溯解法:超时,一般回溯代码会有过多的循环嵌套,中间结果经过多次重复计算造成超时。

 int climbStairs(int n) {
if (n == 1) return 1;
if (n == 2) return 2;
return climbStairs(n-1) + climbStairs(n-2);
}

2、标准DP:(AC)

 public int climbStairs(int n) {
if(n == 1) return 1;
else if(n == 2) return 2; int[] record = new int[n + 1];
record[1] = 1;
record[2] = 2;
for(int i = 3 ; i <= n ; i++){
record[i] = record[i-1] + record[i-2];
}
return record[n];
}

3、DP优化,减少变量,AC。每次保存相邻的三个变量即可:这里我还用了一个flag变量做标记进行迭代赋值,网络上的代码省去了flag,先留着,后面熟悉DP了再看看。

 public int climbStairs(int n) {
if(n == 1) return 1;
else if(n == 2) return 2; int first = 1 , second = 2 , three = 0;
boolean flag = true;
for(int i = 3 ; i <= n ; i++){
three = second + first;
if(flag){
first = three;
flag = false;
}else{
second = three;
flag = true;
}
}
return three;
}

[leetcode]_Climbing Stairs的更多相关文章

  1. leetcode先刷_Climbing Stairs

    水的问题. 以为很常见.青蛙跳楼梯.能跳一步可以跳两步,它实际上是一个斐波那契数. 注意.空间O(1). class Solution { public: int climbStairs(int n) ...

  2. [LeetCode] 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

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

  4. [LeetCode] Climbing Stairs (Sequence DP)

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

  5. LeetCode——Climbing Stairs

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

  6. LeetCode:Climbing Stairs(编程之美2.9-斐波那契数列)

    题目链接 You are climbing a stair case. It takes n steps to reach to the top. Each time you can either c ...

  7. leetcode Climbing Stairs python

    class Solution(object): def climbStairs(self, n): """ :type n: int :rtype: int " ...

  8. [Leetcode] climbing stairs 爬楼梯

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

  9. [LeetCode] Climbing Stairs 斐波那契数列

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

随机推荐

  1. javascript delete方法

    学习delete可以参考下面两个博客,写的都很好,本文大部分参考与以下两个博客 http://www.cnblogs.com/windows7/archive/2010/03/28/1698387.h ...

  2. 用SPCOMM 在 Delphi中实现串口通讯 转

      用Delphi 实现串口通讯,常用的几种方法为:使用控件如MSCOMM和SPCOMM,使用API函数或者在Delphi 中调用其它串口通讯程序.利用API编写串口通信程序较为复杂,需要掌握大量通信 ...

  3. String Format for DateTime

    This example shows how to format DateTime using String.Format method. All formatting can be done als ...

  4. 项目管理: Maven 让事情变得简单

    http://maven.apache.org/,  Maven其实就是为java实现的一个构建工具.他比Ant更高端. 目前,绝大多数开发人员都把 Ant 当作 Java 编程项目的标准构建工具.遗 ...

  5. GL_INVALID_VALUE(0X501)

    当android应该打开GPU的支持后, 有些手机会出现黑屏.闪屏等现象. 跟踪控制台, 会打印日志GL_INVALID_VALUE(0X501). 参考资料:http://mobile.riaos. ...

  6. 内网安装ubuntu包

    到http://packages.ubuntu.com搜索包下载下来,  再安装.

  7. 欧几里得算法与扩展欧几里得算法_C++

    先感谢参考文献:http://www.cnblogs.com/frog112111/archive/2012/08/19/2646012.html 注:以下讨论的数均为整数 一.欧几里得算法(重点是证 ...

  8. Basler usb SDK安装在opencv采集图像

    近期,入手一台baslerUSB接口的CCD相机,但是貌似之前图像采集的编程无法调动其摄像头,在网上搜了一下,大家的说法就是安装它的SDK文件包,并且调用它内部函数编写代码.其实新版的Basle相机驱 ...

  9. C++调用WebService服务问题总结

    近期接手一部分C++研发工作,同时与.NET开发小组进行软件功能协作,于是需要调用.NET WebService服务,已完成数据的网络通讯业务功能:结束之余,将手头碰到的问题总结罗列如下: C++如何 ...

  10. VS集成Qt环境搭建

    环境:VS2010 + Qt5.2 关于VS的下载.安装,这里就不再做过多阐述. 一.下载Qt5.2安装包(qt-windows-opensource)与Qt插件(Visual Studio Add- ...