[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 ...
随机推荐
- 20145305 《Java程序设计》第10周学习总结
学习内容总结 网络编程 网络编程就是在两个或两个以上的设备(例如计算机)之间传输数据.程序员所作的事情就是把数据发送到指定的位置,或者接收到指定的数据,这个就是狭义的网络编程范畴.在发送和接收数据时, ...
- Dubbo的Maven打包
Maven项目的依赖,我们可以通过下面命令看到,打包时,会按照这个依赖编译进去对应模块. 参考:http://www.cnblogs.com/ghj1976/p/5336923.html mvn de ...
- python 装饰器和 functools 模块
转自:http://blog.jkey.lu/2013/03/15/python-decorator-and-functools-module/ 什么是装饰器? 在 python 语言里第一次看到装饰 ...
- 我自己做的网站终于上线啦,用tornado做的,求围观 www.yustock.live
跟股票相关的一个小网站~ www.yustock.live
- Chapter Configuration
Chapter Configuration 在Web.config 或App.config的configuration节,插入如下配置: <configuration> …… <co ...
- ASP.NET MVC开发微信(四)
- Redis数据持久化之AOF持久化
一.RDB持久化的缺点创建RDB文件需要将服务器所有的数据库的数据都保存起来,这是一个非常耗费资源和时间的操作,所以服务器需要隔一段时间才能创建一个新的RDB文件,就也是说创建RDB文件的操作不能执行 ...
- cocos2d-lua 3.5 android搭建步骤
cocos2d-lua 3.5 android搭建步骤 如何安装eclipse,jdk,android sdk,ndk这里都不说了,资料很多,而且以前用eclipse搭建cocos2d-x-c++的时 ...
- Android框架 加载图片 库 Picasso 的使用简介
0 说明 现在Android开源库中有许多图片加载框架,本文以picasso为例,总结下开发过程中的一些优化经验,使用的picasso版本如下 compile 'com.squareup.picass ...
- 【Unity Shaders】学习笔记——SurfaceShader(一)认识结构
[Unity Shaders]学习笔记——SurfaceShader(一)认识结构 转载请注明出处:http://www.cnblogs.com/-867259206/p/5595747.html 写 ...