题目描写叙述:

初阶:有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. Wavenet运行

    作者:桂. 时间:2017-05-10  19:17:32 链接:http://www.cnblogs.com/xingshansi/p/6832219.html 一.环境 python3.5 Win ...

  2. 几种常见排序算法之Java实现(插入排序、希尔排序、冒泡排序、快速排序、选择排序、归并排序)

    排序(Sorting) 是计算机程序设计中的一种重要操作,它的功能是将一个数据元素(或记录)的任意序列,重新排列成一个关键字有序的序列. 稳定度(稳定性)一个排序算法是稳定的,就是当有两个相等记录的关 ...

  3. 【iOS】UIWebView HTML5 扩展

    对于不少iOS开发人员来说,HTML5的内容比較陌生. 尤其是UIWebView类的 stringByEvaluatingJavaScriptFromString 方法 让非常多人认为又得学一种新的语 ...

  4. [Codility] CountTriangles

    A zero-indexed array A consisting of N integers is given. A triplet (P, Q, R) is triangular if it is ...

  5. 【Android】3.21 示例21—兴趣点收藏功能

    分类:C#.Android.VS2015.百度地图应用: 创建日期:2016-02-04 简介:介绍如何创建.管理本地收藏的兴趣点数据 详述: (1)新建本地点收藏: (2)查看已收藏本地点: (3) ...

  6. Executor , ExecutorService 和 Executors

    三者的主要区别和关系如下: Executor 和 ExecutorService 这两个接口主要的区别是:ExecutorService 接口继承了 Executor 接口,是 Executor 的子 ...

  7. Apache Commons Lang的StringUtils.isEmpty(STR)和StringUtils.isBlank(STR)

    Apache Commons Lang是常用的基础框架,其中字符串判空在项目中尤为常用,而自己常常忘记他们的区别. package com.nicchagil.test; import org.apa ...

  8. centos7 ping: www.baidu.com: Name or service not known

    [root@www ~]# ping www.baidu.com ping: www.baidu.com: Name or service not known [root@www ~]# 1.网络配置 ...

  9. 搞清tomcat中的编解码

    http://www.xuebuyuan.com/1287083.html *********************************** 经常会被乱码问题搅得头晕脑胀.事实上,乱码问题涉及的 ...

  10. gson 转换 List<Map> 注意事项

    如果list泛型显示指定Map类型, 这时的Map 不能直接转换为 jre自带的 map类型 gson封装了 StringMap 进行转换