题目描写叙述:

初阶:有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. mac下安装mysqlcient 报错

    一.我在mac下pip3安装mysqlclient 报错: pip3 install mysqlclient Collecting mysqlclient Using cached mysqlclie ...

  2. mysql如何查找某字段所在表

    如果是5.0以上的,以root用户连接,可以看到一个叫information_schema的表, 然后只要:use information_schema; select `TABLE_NAME`fro ...

  3. 深入理解Docker Volume(一)

    想要了解Docker Volume,首先我们需要知道Docker的文件系统是如何工作的.Docker镜像是由多个文件系统(只读层)叠加而成.当我们启动一个容器的时候,Docker会加载镜像层并在其上添 ...

  4. vi作者:Bill Joy

    威廉·纳尔逊·乔伊(William Nelson Joy,1954年11月8日-),通称比尔·乔伊(Bill Joy),美国计算机科学家.与Vinod Khosla.Scott McNealy和And ...

  5. 【Android】10.2 使用Android Support Library增强组件功能

    分类:C#.Android.VS2015: 创建日期:2016-02-18 一.简介 Android Support Library提供了一些非常漂亮的附加功能,由于这些库的引用办法都差不多,所以这一 ...

  6. receiver type *** for instance message is a forward declaration

    转自:http://stackoverflow.com/questions/8815200/receiver-type-for-instance-message-is-a-forward-declar ...

  7. android framework-安装samba

    用于在windows下使用souceinsight访问linux中的android源代码. □    apt-get install samba samba-common      #安装samba ...

  8. ny236 心急的C小加 hdoj1051 Wooden Sticks

    心急的C小加 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 C小加有一些木棒,它们的长度和质量都已经知道,需要一个机器处理这些木棒,机器开启的时候需要耗费一个单位的时间 ...

  9. js将时间戳转换成正常的yyyy-m-d格式

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. 用C#写一个多进程监控自动关机工具

    因为据说某server开着就很贵,所以我们跑完测试的job后就要赶紧关机才行,但是测试的job要跑很久,过程中又不需要干什么,所以就得有个守家的,有时候会走很晚.如果有一个自动化关机的工具就好了,当指 ...