LintCode 111 Climbing Stairs
这道题参考了这个网址: http://blog.csdn.net/u012490475/article/details/48845683
/*
首先考虑边界情况,当有1层时,有一种方法。
然后再看2层时,有1+1、和2+0,两种方法。
再看3层时,首先有两种选择:走一步或者走两步。
如果走两步,那后面还剩一步可走;
如果走一步,后面还剩两步可走,后面的方法即可等同于上面的2层情况。
即可归纳出用C(i) = j; 表示n层时有j种可能。
C(1) = 1;
C(2) = 2;
C(3) = C(3-2) + C(3-1); //因为只有两种选择.
C(4) = C(4-2) + C(4-1);
…
C(n) = C(n-2) + C(n-1);
*/
public int climbStairs(int n) {
int a = 1;
int b = 1;
int c = 1;
for(int i = 1; i < n; i++)
{
c = a + b;
a = b;
b = c;
}
return c;
}
上面的是iteration的做法,下面是递归,非常简洁。
public static int climbStairs(int n) {
// write your code here
if (n == 1) return 1;
else if (n == 2) return 2;
else return climbStairs(n - 1) + climbStairs(n - 2);
}
另外看到Python的解法,更为简洁精妙
/*
经典的动态规划问题。每次上一个台阶或者两个台阶,问一共有多少种方法到楼顶。这个实际上就是斐波那契数列的求解。可以逆向来分析问题,如果有n个台阶,那么走完n个台阶的方式有f(n)种。而走完n个台阶有两种方法,先走完n-2个台阶,然后跨2个台阶;先走完n-1个台阶,然后跨1个台阶。所以f(n) = f(n-1) + f(n-2)。
*/
Ref: http://bookshadow.com/weblog/2015/08/23/leetcode-climbing-stairs/
LintCode 111 Climbing Stairs的更多相关文章
- 111. Climbing Stairs 【LintCode easy】
Description You are climbing a stair case. It takes n steps to reach to the top. Each time you can e ...
- [LintCode] 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 ...
- Leetcode: climbing stairs
July 28, 2015 Problem statement: You are climbing a stair case. It takes n steps to reach to the top ...
- 54. Search a 2D Matrix && Climbing Stairs (Easy)
Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This m ...
- Climbing Stairs
Climbing Stairs https://leetcode.com/problems/climbing-stairs/ You are climbing a stair case. It tak ...
- 3月3日(6) Climbing Stairs
原题 Climbing Stairs 求斐波那契数列的第N项,开始想用通项公式求解,其实一个O(n)就搞定了. class Solution { public: int climbStairs(int ...
- leetCode 70.Climbing Stairs (爬楼梯) 解题思路和方法
Climbing Stairs You are climbing a stair case. It takes n steps to reach to the top. Each time you ...
- 【LeetCode练习题】Climbing Stairs
Climbing Stairs You are climbing a stair case. It takes n steps to reach to the top. Each time you c ...
随机推荐
- 安装YouCompleteMe
1. from software center 1 $ apt-get install vim vim-youcompleteme vim-addon-manager 2 $ vam install ...
- Linux入门学习 常用命令
cd命令 功能是切换到指定的目录:命令格式:cd [目录名]有几个符号作为目录名有特殊的含义:"/"代表根目录.".."代表上一级目录."~" ...
- 运动 js
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- LSD-SLAM深入学习(3)-代码解析
前言 在LSD-SLAM深入学习(2)中我们已经对算法进行分析,此处假设读者对于ros的基本操作都已经很熟悉,而且已经编写了一定量的的代码,我们直接上干货.此处分析的程序如下 main_live_od ...
- sql数据查询,在程序中较慢,在MS SQL2005 Management Studio中速度快,情况分析及解决
这两天遇到一个问题,在.net开发的网站,执行sql查询,从sql profiler中监控卡看,执行时间22s. 但是拷出的sql在Management Studio中直接执行,时间仅4ms. 解决方 ...
- 随部分div增高总的div也随着增高
实现效果: 随着尺码框选项变多,高度也就增加,上边总体的大框高度也增加,简单的样式代码如下 代码: <div style=“height: auto; overflow: hidden;”> ...
- Ceph剖析:Leader选举
作者:吴香伟 发表于 2014/09/11 版权声明:可以任意转载,转载时务必以超链接形式标明文章原始出处和作者信息以及版权声明 Paxos算法存在活锁问题.从节点中选出Leader,然后将所有对数据 ...
- 转载:分布式系统的CAP理论
原文转载Hollis原创文章:http://www.hollischuang.com/archives/666 2000年7月,加州大学伯克利分校的Eric Brewer教授在ACM PODC会议上提 ...
- javascript:history.go()和History.back()的区别(转载)
javascript:history.go()和History.back()的区别 <input type=button value=刷新 onclick="window. ...
- LINQ - 在Where條件式中使用in與not in
希望对大家在以后的项目中能用到,我也是在项目中碰到了这个问题: 算算時間,接觸LINQ也有一個月的時間了,可以算是落伍兼新生,不過最近在寫專案的時候,遇到了在LINQ的Where條件式中要如何使用in ...