我敢保证这道题是在今早蹲厕所的时候突然冒出的解法。第一次接触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. [SQL] 如何在SQL Server2005数据库中检查一个表是否存在,如存在就删除表记录,如不存在就建表.

    . 检索 dbo.sysobjects表, select count(*) from dbo.sysobjects where xtype='U' and Name = '你的表名' . 根据返回的结 ...

  2. [SQL]SQL语言入门级教材_SQL语言基本语句介绍(四)

    SQL语言基本语句介绍 • 表的建立 关系数据库的主要特点之一就是用表的方式组织数据.表是SQL语言存放数据.查找数据以及更新数据的基本数据结构.在SQL语言中,表有严格的定义,它是一种二维表,对于这 ...

  3. Codeforces 450D Jzzhu and Cities [heap优化dij]

    #include<bits/stdc++.h> #define MAXN 100050 #define MAXM 900000 using namespace std; struct st ...

  4. cocos2dx一个场景添加多个层

    首先创建两个layer,以下是头文件 #pragma once#include "cocos2d.h"USING_NS_CC;class BackgroundLayer : pub ...

  5. springMVC spring hibernate pom.xml备份

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  6. python通过163邮箱发送邮件

    from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText import smtplib i ...

  7. Grunt 之通配符

    在描述源码路径的时候,经常有一些特殊的奇怪的要求.Grunt 通过内建的 node-glob 和 minimatch 库提供了文件名的扩展机制. 常见的通配符如下: * 匹配除了 / 之外的任意数量的 ...

  8. Python安装Selenium3

    概述 2016.10.13,Selenium3.0正式发布,官方说明如下: The major change in Selenium 3.0 is we're removing the origina ...

  9. iptable怎么用?

    iptables -A FORWARD -s 10.0.0.0/8  -p tcp  --dport  80  -j DROP [拒绝转发来自10.0.0.0/8网段,目的端口是80的数据包]

  10. Eclipse插件开发之基础篇(4) OSGi框架

    转载出处:http://www.cnblogs.com/liuzhuo. 1. 什么是OSGi框架 OSGi(Open Service Gateway Initiative)框架是运行在JavaVM环 ...