[leetcode]_Climbing Stairs
我敢保证这道题是在今早蹲厕所的时候突然冒出的解法。第一次接触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的更多相关文章
- leetcode先刷_Climbing Stairs
水的问题. 以为很常见.青蛙跳楼梯.能跳一步可以跳两步,它实际上是一个斐波那契数. 注意.空间O(1). class Solution { public: int climbStairs(int n) ...
- [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: climbing stairs
July 28, 2015 Problem statement: You are climbing a stair case. It takes n steps to reach to the top ...
- [LeetCode] Climbing Stairs (Sequence DP)
Climbing Stairs https://oj.leetcode.com/problems/climbing-stairs/ You are climbing a stair case. It ...
- 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: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 ...
- leetcode Climbing Stairs python
class Solution(object): def climbStairs(self, n): """ :type n: int :rtype: int " ...
- [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] Climbing Stairs 斐波那契数列
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
随机推荐
- ubuntu入门知识
1.linux系统发展历史 unix -> Linux -> ubuntu linux发展轨迹图 2.ubuntu下载和安装 推荐使用长期支持版本: 10.04,12.04,14.04或L ...
- [ActionScript 3.0] AS3.0 获取文本的明暗度
/** * 获取文字的明暗值 * @param t 文字 * @return Number */ function getDensity(t:String):Number { var ttf:Text ...
- DELPHI下的SOCK编程(转)
DELPHI下的SOCK编程 本文是写给公司新来的程序员的,算是一点培训的教材.本文不会涉及太多的编程细节,只是简单讲解在DELPHI下进行Winsock编程最好了解的知识. 题外话:我认为 ...
- sql server 根据执行计划查询耗时操作
with QS as( select cp.objtype as object_type, /*类型*/ db_name(st.dbid) as [database], /*数据库*/ object_ ...
- 如何让你的Python代码更加pythonic ?
pythonic如果翻译成中文的话就是很python.很+名词结构的用法在中国不少. 以下为了简略,我们用P表示pythonic的写法,NP表示non-pythonic的写法,当然此P-NP非彼P-N ...
- Ext.Form 自动填写表单内容
前台: 表单必须含有name属性 if (action == 'edit' || action == 'show') { MyForm1.getForm().load({ url: '/data/cu ...
- windows 测试数据库的连接状况-udl方法
udl是windows系统上,用于测试数据库的连接状态的测试软件. 使用方法: 1.建立一个空白文本 2.将文件的后缀名更改为*.udl 即可 文件内容一定为空 3.选择windows的“提供 ...
- 紧张:飞测独家のJmeter秘籍,限量发放
飞测说:数月前,小怪我牺牲了周末时间,做了fiddler的扩展开发,从fiddler将请求导出,保存为jmx格式的文件,然后使用jmeter来调用.随后,小怪和同事(心&阳)一同研究jmete ...
- FFTW库在VS 2010中的使用方法
一.FFTW库简介(from百度百科) FFTW ( the Faster Fourier Transform in the West) 是一个快速计算离散傅里叶变换的标准C语言程序集,其 ...
- Quick Sort
class Program { static int[] a = new int[] { 6, 1, 2, 7, 9, 3, 4, 5, 10, 8 }; int n; static void Mai ...