climbing stairs(爬楼梯)(动态规划)
You are climbing a stair case. It takes n steps to reach to the top.
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
Note: Given n will be a positive integer.
Example 1:
Input: 2
Output: 2
Explanation: There are two ways to climb to the top.
1. 1 step + 1 step
2. 2 steps
Example 2:
Input: 3
Output: 3
Explanation: There are three ways to climb to the top.
1. 1 step + 1 step + 1 step
2. 1 step + 2 steps
3. 2 steps + 1 step
爬楼梯,一次可以爬一级或者两级楼梯。问爬到n级楼梯有多少中方法。
分析:爬到n,可以是从n-1级楼梯一次爬上来,也可以是从n-2级一次走两步上来(不能从n-2走一步再走一步,因为走一步就会去到n-1级,重复)。所以有公式f(n)=f(n-1)+f(n-2)。
递归是可以解决的,但是递归会出现超时的情况。
所以使用动态规划。动态规划也没有什么特别之处,就是把一个大问题分解成许多个子问题,子问题解决了,大问题也就解决了。比如这里的f(n),可以先求f(3),再根据状态方程求后一项,。。。,。这就是动态规划。
递归都可以转成动态规划。
动规解题的一般思路
1. 将原问题分解为子问题
- 把原问题分解为若干个子问题,子问题和原问题形式相同或类似,只不过规模变小了。子问题都解决,原问题即解决。
- 子问题的解一旦求出就会被保存,所以每个子问题只需求 解一次。
2.确定状态
- 在用动态规划解题时,我们往往将和子问题相关的各个变量的一组取值,称之为一个“状 态”。一个“状态”对应于一个或多个子问题, 所谓某个“状态”下的“值”,就是这个“状 态”所对应的子问题的解。
- 所有“状态”的集合,构成问题的“状态空间”。“状态空间”的大小,与用动态规划解决问题的时间复杂度直接相关。
3.确定一些初始状态(边界状态)的值
以“爬楼梯”为例,初始状态就是f(1),f(2),值就是数字值。
4. 确定状态转移方程
定义出什么是“状态”,以及在该“状态”下的“值”后,就要找出不同的状态之间如何迁移――即如何从一个或多个“值”已知的 “状态”,求出另一个“状态”的“值”(递推型)。状态的迁移可以用递推公式表示,此递推公式也可被称作“状态转移方程”。
从递推公式出发,就可以先求出小问题的解,最后大问题的解就出来了。如先求出f(3),f(4)...
代码:
class Solution {
public int climbStairs(int n) {
if(n<=0) return 0;
if(n==1) return 1;
if(n==2) return 2;
/*
根据规则可以知道f(n)=f(n-1)+f(n-2);但是不能用递归,因为数字很大时,递归会超时。
这里其实是动态规划,思路还是根据上面的公式。一直从前往后加。自己在草稿纸上多写几项就知道规律了。
这里:one_step_before,表示f(n-1),two_step_before表示f(n-2).total表示f(n)。
比如,f(1)=1,f(2)=2,则f(3)=3,此时要算下一个f(4),f(4)=f(3)+f(2),所以对于f(4)而言,two_step_before=one_step_before(f2),one_step_before=total(f3);
*/
int one_step_before=2;
int two_step_before=1;
int total=0;
for(int i=3;i<=n;i++){
total=one_step_before+two_step_before;
two_step_before=one_step_before;
one_step_before=total;
}
return total;
}
}
climbing stairs(爬楼梯)(动态规划)的更多相关文章
- Climbing Stairs爬楼梯——动态规划
题目描写叙述: 初阶:有n层的台阶,一開始你站在第0层,每次能够爬两层或者一层. 请问爬到第n层有多少种不同的方法? 进阶:假设每次能够爬两层.和倒退一层,同一个位置不能反复走,请问爬到第n层有多少种 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- Leetcode 70. Climbing Stairs 爬楼梯 (递归,记忆化,动态规划)
题目描述 要爬N阶楼梯,每次你可以走一阶或者两阶,问到N阶有多少种走法 测试样例 Input: 2 Output: 2 Explanation: 到第二阶有2种走法 1. 1 步 + 1 步 2. 2 ...
- 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 ...
- [Leetcode] climbing stairs 爬楼梯
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- [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 ...
随机推荐
- 最简单的基于librtmp的示例:发布H.264(H.264通过RTMP发布)
===================================================== 最简单的基于libRTMP的示例系列文章列表: 最简单的基于librtmp的示例:接收(RT ...
- Ubuntu15.10下制作Linux 操作系统优盘启动盘
上次电脑出现了一些问题,于是不得不重新装机了.下面就跟大家分享一下我在Ubuntu下制作优盘启动盘的一些心得. 准备原料 我这里用到的是 镜像文件是:debian-8.3.0-amd64-DVD-2. ...
- Android Studio(AS)-->导入项目
1:首先,你必须要有一个工程(Project), 才可以打开项目(Module); (注意:Eclipse中的Workspace对应Android Studio 中的Project, Eclipse中 ...
- Android进阶(三)android httpClient 支持HTTPS的访问方式
项目中Android https请求地址遇到了这个异常(无终端认证): javax.net.ssl.SSLPeerUnverifiedException: No peer certificate 是S ...
- Java线程的状态
Java线程的状态 线程对象在不同的运行时期有不同的状态,状态信息就存在于Thread中的State枚举中,如下所示: public enum State { /** * 至今尚未启动的线程处于这种状 ...
- pig运行方法:本地与云上
pig脚本 放在本地当前目录(键入pig命令时,所处的目录),然后用进入grunt,用run或者exec调用 1云运行: 键入pig进入grunt,用run命令运行当前目录脚本.(或者外部用pig - ...
- 下载android5.0源码
方法还是与之前我介绍的下载源码的方法一样,但是repo需要更新一下,否则可能会出现以下错误: type commit tag v1.12.16 tagger Conley Owens <cco3 ...
- python 去掉 pyc
python 去掉 .pyc 在开发的机器上(Ubuntu),python自动生成的pyc文件太影响心情,把下面的语句添加到 /etc/profile中: # do not produce .pyc ...
- STL(标准模板库)理论基础,容器,迭代器,算法
基本概念 STL(Standard Template Library,标准模板库)是惠普实验室开发的一系列软件的统称.现然主要出现在C++中,但在被引入C++之前该技术就已经存在了很长的一段时间. ...
- (二)plist的使用和序列帧动画
六.plist的使用方法: iOS的程序在安装在手机上以后会把全部资源文件集成在一个文件夹中,这种文件集合称为bundle,对于一般的工程,只有一个bundle,即mainbundle,因此可以通过b ...