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 ...
随机推荐
- 2 Add Two Numbers
// Java public ListNode addTwoNumbers(ListNode l1, ListNode l2) { return add(l1, l2, false); } priva ...
- LINUX 更新
sudo apt-get dist-upgrade,更新所有的软件
- UIAlertViewController的使用
UIAlertViewController是苹果自带的信息提示框,仅在iOS8.0以后可以使用 NS_CLASS_AVAILABLE_IOS(8_0) @interface UIAlertContro ...
- PL/SQL远程连接Oracle的方式,无需安装客户端
第一次用PL/SQL Developer这个非官方的软件,遇到了种种困难,幸好通过万能的Google已经全部解决,记录下来备察. 前提是保证你的远程oracle服务器一切正常. 微软客户端设置 使 ...
- android创建桌面快捷方式(启动目标非项目的启动页)
1.布局文件中,目标Activity加入以下filter <intent-filter> <action android:name="an ...
- error: src refspec master does not match any. 错误处理办法
自从上次学了git之后,很少用.今天在使用 本地仓库使用如下命令初始化: $ git init 之后使用如下命令添加远程库: $ git remote add origin git@github.co ...
- Asp.Net MVC4入门指南(9):查询详细信息和删除记录
在本教程中,您将查看自动生成的Details和Delete方法. 查询详细信息和删除记录 打开Movie控制器并查看Details方法. public ActionResult Details(int ...
- DUT Star Round2
A.Zeratu的军训游戏 Problems: 开灯问题,问无数次操作之后第n盏灯的状态 Analysis: cj:平方数有奇数个约数 Tags: Implementation B.Zeratud的完 ...
- js 根据名字获取cookie 的方法
function getcookie(c_name) { if (document.cookie.length > 0) { c_start = document.cookie.indexOf( ...
- 关于js单页面实现跳转原理以及利用angularjs框架路由实现单页面跳转
还记得我们刚开始学习html时使用的锚节点实现跳转吗? <a href="#target">我想跳转至目标位置</a> <p>第一条</p ...