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权威指南 (5) - 使用Buffer类处理二进制数据

    5.1 创建Buffer对象 / 705.2 字符串的长度与缓存区的长度 / 725.3 Buffer对象与字符串对象之间的相互转换 / 74 5.3.1 Buffer对象的toString方法 / ...

  2. position: absolute 的元素自动对齐父元素 border 外边缘

    Position with border outer edge CSS box-flex align-items justify-content

  3. 将cocos的app直接在我的设备上测试运行

    首先,你要有一个写好了的,准备在真机上测试的cocos程序. 1.设置ARC,设置的过程在另外一篇博文上有写. 2.在Target的Build Setting里面 找到Valid Archs 删除里面 ...

  4. Hdu 3966-Aragorn's Story LCT,动态树

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3966 Aragorn's Story Time Limit: 10000/3000 MS (Java/Ot ...

  5. [LeetCode] Word Break 解题思路

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  6. c#委托和事件(上)

    C# 中的委托和事件 引言 委托 和 事件在 .Net Framework中的应用非常广泛,然而,较好地理解委托和事件对很多接触C#时间不长的人来说并不容易.它们就像是一道槛儿,过了这个槛的人,觉得真 ...

  7. form与action之setter与getter(转)

    对于表单提交数据给action时候,可以简单的用setter与getter函数实现值的传递. 例如在jsp里有这么个form: <s:form action="login"& ...

  8. Linux Resin 安装

    1 Resin 下载 Resin 官方下载网址. 最新版下载 resin-4.0.36.tar.gz(免费版) resin 安装须要提前配置好jdk.配置jdk请看上面文章 2 Resin 安装 (1 ...

  9. 基于Lucene3.5.0怎样从TokenStream获得Token

    通过学习Lucene3.5.0的doc文档,对不同release版本号 lucene版本号的API修改做分析.最后找到了有价值的修改信息. LUCENE-2302: Deprecated TermAt ...

  10. LINUX 性能 测试 优化工具

    监控 测试 优化