[LeetCode OJ]-Climbing Stairs
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的更多相关文章
- LeetCode OJ——Climbing Stairs
http://oj.leetcode.com/problems/climbing-stairs/ 走台阶的题目,转换出数学模型之后,就是Fibonacci数列.后一个数等于前两个数的和. 递归版本超时 ...
- [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 ...
- [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 ...
- leetCode 70.Climbing Stairs (爬楼梯) 解题思路和方法
Climbing Stairs You are climbing a stair case. It takes n steps to reach to the top. Each time you ...
- 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 ...
- Leetcode#70. Climbing Stairs(爬楼梯)
题目描述 假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数. 示例 1: 输入: 2 输出: 2 解 ...
- 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 ...
- [leetcode] 14. Climbing Stairs
这道题leetcode上面写着是DP问题,问题是我一开始写了个简单的递归结果直接超时,所以没办法只好拿迭代来做了.题目如下: You are climbing a stair case. It tak ...
- LeetCode 70 Climbing Stairs(爬楼梯)(动态规划)(*)
翻译 你正在爬一个楼梯. 它须要n步才干究竟顶部. 每次你能够爬1步或者2两步. 那么你有多少种不同的方法爬到顶部呢? 原文 You are climbing a stair case. It tak ...
随机推荐
- ORACLE EXP-00011:表不存在的分析和解决方案
解决方法: ~~看下数据库的延迟段创建参数 SQL> show parameter DEFERRED_SEGMENT_CREATION NAME ...
- main函数的3个参数
最早是在MCU中使用C语言,MCU中的main函数没有参数. 后来在PC上面使用C语言,一般教程都写的是main函数有2个参数: int main(int argc, const char **arg ...
- C#中同时使用Lambda表达式和递归
Func<object, int> RTFunc = (RTFuncRT) => { return (RTFuncRT as Func<object, int>)(RTF ...
- Java中如何使用Redis做缓存
基本功能测试 1.程序基本结构 2.主要类 1)功能类 package com.redis; import java.util.ArrayList; import java.util.Iterator ...
- 用Seam实现:图片上传 + 保存到数据库 + 从数据库读出图片并显示到页面中
上传图片并保存到数据库 seam给我们提供了 s:fileUpload 标签以完成文件上传功能.使用该标签时,要在web.xml中声明一个Seam的过滤器: <filter> <fi ...
- 【设计模式 - 15】之解释器模式(Interpreter)
1 模式简介 解释器模式允许我们自定义一种语言,并定义一个这种语言的解释器,这个解释器用来解释语言中的句子.由于这种模式主要用于编译器的编写,因此在日常应用中不是很常用. 如果一种特定类型的 ...
- Activity透明/半透明效果的设置transparent(两种实现方法)
两种方法实现Activity透明/半透明效果的设置,代码思路很有调理,感兴趣的朋友可以参考下,希望本文可以帮助到你 方法一:res/values文件夹下建立styles.xml: 复制代码代码如下 ...
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(30)-本地化(多语言)
原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(30)-本地化(多语言) 我们的系统有时要扩展到其他国家,或者地区,需要更多的语言环境,微软提供了一些解决 ...
- C# 自动登录网页,浏览页面【转载】
需求:客户的数据同时存在在另外一个不可控的系统中,需要和当前系统同步. 思路:自动登录另外一个系统,然后抓取数据,同步到本系统中. 技术点:模拟用户登录:保存登录状态:抓取数据 /// <sum ...
- android 32 Gallery:横着滚动的列表
Gallery:横着滚动的列表 mainActivity.java package com.sxt.day05_01; import java.util.ArrayList; import java. ...