You are climbing a stair case. It takes n steps to reach to the top.

Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?

一共有n阶楼梯,每次只能爬1阶或2阶,问爬到顶端一共有多少种方法

方法一:

利用二叉树的思想,对于n=3时有3种方法,有几个叶子节点便有几种方法

 void climb( int remainder, int &way)
{
if(remainder== || remainder==)
{
way++;
return;
} climb(remainder-, way);
climb(remainder-, way);
return;
} class Solution {
public:
int climbStairs(int n) {
int result=;
climb(n, result);
return result;
}
};

但是这种方法对于n比较大时会超时,在测试用例中对于n=38就会TLE(Time Limit Exceed)。

方法二:

总结规律,通过以下数据,我们发现

way(1)=1

way(n) = way(n-1) + Fibonacci(n-1)    n=2,3,4,5,6,....

 class Solution {
public:
int climbStairs(int n) {
int result=;
int Fibonacci_0=, Fibonacci_1=, temp;
for(int i=; i<n; i++)
{
result += Fibonacci_1;
temp = Fibonacci_1;
Fibonacci_1 = Fibonacci_0 + Fibonacci_1;
Fibonacci_0 = temp; }
return result;
}
};

对于leetcode中所有的测试数据都可以通过

[LeetCode OJ]-Climbing Stairs的更多相关文章

  1. LeetCode OJ——Climbing Stairs

    http://oj.leetcode.com/problems/climbing-stairs/ 走台阶的题目,转换出数学模型之后,就是Fibonacci数列.后一个数等于前两个数的和. 递归版本超时 ...

  2. [LeetCode] 70. Climbing Stairs 爬楼梯问题

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...

  3. [LeetCode] 70. Climbing Stairs 爬楼梯

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...

  4. leetCode 70.Climbing Stairs (爬楼梯) 解题思路和方法

    Climbing Stairs  You are climbing a stair case. It takes n steps to reach to the top. Each time you ...

  5. 42. leetcode 70. Climbing Stairs

    70. Climbing Stairs You are climbing a stair case. It takes n steps to reach to the top. Each time y ...

  6. Leetcode#70. Climbing Stairs(爬楼梯)

    题目描述 假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数. 示例 1: 输入: 2 输出: 2 解 ...

  7. LN : leetcode 70 Climbing Stairs

    lc 70 Climbing Stairs 70 Climbing Stairs You are climbing a stair case. It takes n steps to reach to ...

  8. [leetcode] 14. Climbing Stairs

    这道题leetcode上面写着是DP问题,问题是我一开始写了个简单的递归结果直接超时,所以没办法只好拿迭代来做了.题目如下: You are climbing a stair case. It tak ...

  9. LeetCode 70 Climbing Stairs(爬楼梯)(动态规划)(*)

    翻译 你正在爬一个楼梯. 它须要n步才干究竟顶部. 每次你能够爬1步或者2两步. 那么你有多少种不同的方法爬到顶部呢? 原文 You are climbing a stair case. It tak ...

随机推荐

  1. Node.js权威指南 (11) - 加密与压缩

    11.1 加密与解密处理 / 295 11.1.1 crypto模块概述 / 295 11.1.2 散列算法 / 296 11.1.3 HMAC算法 / 297 11.1.4 公钥加密 / 29811 ...

  2. HDU-1527 取石子游戏

    http://acm.hdu.edu.cn/showproblem.php?pid=1527 交换  :可实现. if( n < m ) { n^=m; m^=n; n^=m; } (三)尼姆博 ...

  3. oracle查询语句【转载】

    建立的表: 表名:REGIONS 序号 列名 数据类型 长度 小数位 标识 主键 允许空 默认值 说明 1 REGION_ID NUMBER 是 否 2 REGION_NAME VARCHAR2 25 ...

  4. DSP知识

    自己认为是问题的问题,时常更新,为了记录学习的点点滴滴. 1.什么是boot loader ? DSP 的速度尽快,EPROM 或flash 的速度较慢, 而DSP 片内的RAM很快, 片外的RAM也 ...

  5. jQuery 数据 DOM 元素 核心 属性

    jQuery 参考手册 - 数据 .clearQueue() 从序列中删除仍未运行的所有项目 .clearQueue(queueName) $("div").clearQueue( ...

  6. Delphi 对象的创建(create)与释放(free/destory)

    Delphi 对象的创建(create)与释放(free/destory) 1.Create参数为:nil/self/application的区别,最好能看到实际效果的区别 例如: My := TMy ...

  7. Matlab编程-图形处理功能

    绘图功能最基本的命令行:plot(y). 二维图形: (1) >> y=rand(100,1); >> plot(y) y是随机的实向量,以生成y的索引为横坐标,y为纵坐标绘图 ...

  8. 【转】SVN linux命令及 windows相关操作(二)

    转自这里:http://www.uml.org.cn/pzgl/200904246.asp 1 安装及下载client 端 2 什么是SVN(Subversion)? 3 为甚么要用SVN? 4 怎么 ...

  9. (C#)与Windows用户账户信息的获取

    Console.WriteLine(Environment.UserName); //计算机NetBIOS名称 Console.WriteLine(Environment.MachineName); ...

  10. [AngularJS + Webpack] Production Setup

    Using Angular with webpack makes the production build a breeze. Simply alter your webpack configurat ...