题目描写叙述:

初阶:有n层的台阶,一開始你站在第0层,每次能够爬两层或者一层。

请问爬到第n层有多少种不同的方法?

进阶:假设每次能够爬两层。和倒退一层,同一个位置不能反复走,请问爬到第n层有多少种不同的方法?

解题思路:

初阶:一维动态规划。爬楼梯数目事实上是一个斐波拉契数列。

假定f[i] 表示是爬到第i层的方法,那么f[i] = f[i-1] + f[i-2] //第i层的方法数目等于第i-1层数目加上第i-2层数目。

初值:f[0] = 1, f[1] = 1。 //最開始没有爬和第一层的方法数目为1.

输出:f[n] 爬到第n层的方法数目。

实现方法两种:迭代法、递归法

递归法:时间复杂度:O(n) ,空间复杂度:O(n)

迭代法:时间复杂度:O(n),空间复杂度:O(1)

//递归法

class Solution {
public:
/**
* @param n: An integer
* @return: An integer
*/
int climbStairs(int n) {
// write your code here
vector<int> ans(n+1);
ans[0] = 1;
ans[1] = 1;
for(int i=2; i<=n; i++)
ans[i] = ans[i-1] + ans[i-2];
return ans[n];
}
};
//迭代法

class Solution {
public:
/**
* @param n: An integer
* @return: An integer
*/
int climbStairs(int n) {
// write your code here
int prev = 0;
int cur = 1;
for(int i=1; i<=n; i++)
{
int tmp = cur;
cur += prev;
prev = tmp;
}
return cur;
}
};

进阶:參考这里

主要是倒退一层。这个地方可能会违背动态规划无后效性的原则。

那么我们要怎么转化呢?

由条件:同一个位置不能反复走。我们能够知道假设要退步的话,不能退两层以上,由于用两步退两层再一步前进两层,那就会走同样的位置。所以我们最多仅仅能退后一步。

那么题目的条件就能够转换两种情况。

a.跳两层(前进两层)。

b.退一层跳两层 (前进一层)。





1. State:

f[i][0] 表示最后一步是跳两层情况下爬到第i层的方法数目。

f[i][1] 表示最后是一步是退一层跳两层的情况下爬到第i层的方法数目。

2. Function:

f[i+1][1] = f[i][0] 最后一步是退一层跳两层的情况下爬到第i+1层的方法数目
等于 从第i层情况a的数目跳两层退一层。

这里不能考虑第i层的情况b的方法数,由于第i层情况b的数目是从第i+1层退一步得到的。

f[i+2][0] = f[i][0]+f[i][1] 最后一步是退一层跳两层的情况下爬到第i+2层的方法数目
等于 第i层全部情况跳两层。

3. Intialize:

f[0][0]=1初始化最開始没有爬的方法数目为1.

4. Answer:

f[n][0]+f[n][1] 爬到第n层a、b两种不同的方法的总和

Climbing Stairs爬楼梯——动态规划的更多相关文章

  1. climbing stairs(爬楼梯)(动态规划)

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

  2. [LeetCode] 70. 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] 70. 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] Min Cost Climbing Stairs 爬楼梯的最小损失

    On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once you pay ...

  5. [LeetCode] 746. Min Cost Climbing Stairs 爬楼梯的最小损失

    On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once you pay ...

  6. Leetcode 70. Climbing Stairs 爬楼梯 (递归,记忆化,动态规划)

    题目描述 要爬N阶楼梯,每次你可以走一阶或者两阶,问到N阶有多少种走法 测试样例 Input: 2 Output: 2 Explanation: 到第二阶有2种走法 1. 1 步 + 1 步 2. 2 ...

  7. LeetCode 70. Climbing Stairs爬楼梯 (C++)

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

  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]70. Climbing Stairs爬楼梯

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

随机推荐

  1. django中数据库的相关操作

    一.使用环境 python2.7,django>1.7 二.数据库进行配置 在setting文件中进行修改 1.找到DATABASES DATABASES = { 'default': { 'E ...

  2. 如何发布Node模块到NPM社区

    “学骑自行车最快的方式就是先骑上去” 一.安装node和npm 1.一种是通过编译node源文件安装node(注意:需要Python 2.6或2.7已经安装) $ wget http://nodejs ...

  3. object-c 获得目录(包括子目录)下所有文件和文件夹路径

    void getAllPathNameInDirectory(vector<string>&filePathList,vector<string>&direct ...

  4. linux下man手册简介

    Linux提供了丰富的帮助手册,当你需要查看某个命令的参数时不必到处上网查找,只要man一下即可.Linux 的man手册共有以下几个章节: 1.Standard commands (标准命令)2.S ...

  5. javascript中字符串与数组互转的方法分享

    说明:1.join()方法:用于把数组中的所有元素放入一个字符串,元素是通过指定的分隔符进行分隔的.指定分隔符方法join("$");其中$可以是任意字符2.split()方法:用 ...

  6. 简易C#动态加载dll(实现插件化)

    可以通过该方法来实现程序插件化. 假设A,B两个类,A类为宿主,B类为插件需要加载到A类中: class Program { public interface IHellow { void Hello ...

  7. 【Android】5.6 弹出菜单(PopUp Menus)

    分类:C#.Android.VS2015: 创建日期:2016-02-07 一.简介 功能描述:用户单击按钮弹出菜单.当用户选择一个菜单项,会触发MenuItemClick事件并让弹出的菜单消失:如果 ...

  8. 在windows下codeblocks中配置pthread库

    转自:http://blog.csdn.net/u013172314/article/details/50846198 如果添加方法不正确,可能会出现pthread_create’未定义的引用,所以下 ...

  9. 黑客编程教程(八)编写NT服务

    先介绍一下什么是NT服务,实际上就是一个可以在系统启动时自动在一定身份下启动的,伴随着系统长期存在的进程. 一个NT服务有三部分构成: :Service Control Manager(SCM) 每个 ...

  10. jQuery.extend()中的布尔值的作用

    jQuery.extend()中,第一个参数可以添加一个布尔值,表示是否深层拷贝,默认是false.但如果设置为true,则表示深层拷贝. 下面来看看什么是深层拷贝: <script type= ...