我敢保证这道题是在今早蹲厕所的时候突然冒出的解法。第一次接触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. [Other] 自定义MIME类型支持FLV的相关设置

    刚测试知道为何服务器无法播放flv的原因,特此记录而已. 网络空间支持FLV的相关设置,就是自定义一个MIME类型,一般虚拟主机管理里面都有这个选项 自定义MIME类型 扩展名: .flv MIME类 ...

  2. CRM SQL 创建活动 ActivityPointer

    只是插入的任务,邮件,约会之类的没有研究,以下是官方文档:https://msdn.microsoft.com/zh-cn/library/gg334533.aspx /* 1 实体名 new_xxx ...

  3. javascript delete方法

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

  4. Linux命令 find和mv的结合使用:查找文件,移动到某个目录

    显示前十个文件 [root@localhost smgpbi]# ls -1 | sort -u | head -10 1.首先查看文件个数,进入所在的文件 # find . -name " ...

  5. Cardinality Feedback

    该特性主要针对 统计信息陈旧.无直方图或虽然有直方图但仍基数计算不准确的情况, Cardinality基数的计算直接影响到后续的JOIN COST等重要的成本计算评估,造成CBO选择不当的执行计划 O ...

  6. php 使用pathinfo(), parse_url(), basename()解析URL

    本文章向大家介绍解析URL的三种方法,分别为pathinfo()方法.parse_url()方法和basename()方法.每个方法都列举了一个实例,通过实例更容易理解这三个函数的使用方法和技巧,需要 ...

  7. Android:Activity的跳转

    // 实际开发中常用的方法 Intent intent = new Intent(); intent.setClass(MainActivity.this, LoginActivity.class); ...

  8. 解决eclipse中出现Resource is out of sync with the file system问题

    解决eclipse中出现Resource is out of sync with the file system问题 . 分类: 嵌入式开发平台和环境相关 2011-12-27 16:18 4872人 ...

  9. 【LeetCode】84. Largest Rectangle in Histogram

    Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...

  10. Swift学习(四)常量&变量&基础数据类型

    常量和变量 常量: 使用let关键词来声明一个常量 所指向的是一个特定类型的值,如数字10或者字符”hello”,常量的值是不能够被二次修改的 编程时使用常量能够让代码看起来更加安全和简洁! let ...