[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 ...
随机推荐
- [ActionScript3.0] 深表复制
function clone(obj:Object):Object{ var byteArray:ByteArray = new ByteArray(); byteArray.writeObject( ...
- shell 统计 awk
time awk '{a[$1]++}END{for(i in a){printf("%d\t%s\n",a[i],i)}}' access.log | sort -nr | he ...
- 【转】开源性能测试工具 - Apache ab 介绍
版权声明:本文可以被转载,但是在未经本人许可前,不得用于任何商业用途或其他以盈利为目的的用途.本人保留对本文的一切权利.如需转载,请在转载是保留此版权声明,并保证本文的完整性.也请转贴者理解创作的辛劳 ...
- OC基础(20)
Protocol基本概念 Protocol注意事项 Protocol类型限制 代理设计模式 *:first-child { margin-top: 0 !important; } body > ...
- MFC学习 画图设置字体按钮风格
修改按钮样式时, 设置按钮关联哪个按钮类, 按钮类是自己写的, 从CButton继承, 重写DrawItem可修改按钮样式. 代码中包括画线, 点, 圆, 设置这些的样式, 如线粗, 颜色, 字体. ...
- python学习(三):matplotlib学习
前言:matplotlib是一个python的第三方库,里面的pyplot可以用来作图.下面来学习一下如何使用它的资源. 一.使用前 首先在python中使用任何第三方库时,都必须先将其引入.即: i ...
- FPS学习记录
最近在网上查了一些FPS的相关知识,在此和大家一起分享.FPS(Frames Per Second):每秒传输帧数,它是图像领域中的一个术语. Frames Per Second更确切的解释是“每秒中 ...
- 转载 radio值获取
选择控件:select ,radio,checkbox之用jquery获取选中值的小结 博客分类: jQuery select下拉框radio单选按钮checkbox多选框jquery获取选中的值 ...
- Effective Modern C++翻译(7)-条款6:当auto推导出意外的类型时,使用显式的类型初始化语义
条款6:当auto推导出意外的类型时,使用显式的类型初始化语义 条款5解释了使用auto来声明变量比使用精确的类型声明多了了很多的技术优势,但有的时候,当你想要zag的时候,auto可能会推导出了zi ...
- 翻译「C++ Rvalue References Explained」C++右值引用详解 Part8:Perfect Forwarding(完美转发):解决方案
本文为第八部分,目录请参阅概述部分:http://www.cnblogs.com/harrywong/p/cpp-rvalue-references-explained-introduction.ht ...