[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 ...
随机推荐
- jQuery EasyUI + struts2.3 + mongoDB 列表查询翻页JAVA样例
列表界面: 主要实现方式:前台组合json格式查询条件,提交至后台解析处理 一.前台搜索脚本 String.prototype.replaceAll = function (s1, s2) { ...
- 【转】Android Building System 总结 - 一醉千年 - CSDN博客
原文网址:http://www.360doc.com/content/15/0314/23/1709014_455175716.shtml Android Building System 总结 收藏 ...
- TC358746AXBG/748XBG 桥接器说明
为什么需要这个mipi csi-2 bridge 芯片,由于我们用的sensor 芯片是美光的MT9m021,这颗芯片并不支持MIPI 下面是网上摘录的说明可能解释这个问题: Because of t ...
- linux OpenOffice
1.下载所需的安装包 /project/openofficeorg.mirror/4.0.1/binaries/zh-CN/Apache_OpenOffice_4.0.1_Linux_x86-64_ ...
- [置顶] [MATLAB技术贴]漫谈MATLAB矩阵转置
矩阵转置是matlab最基本的操作了,但这个基本操作,也是很多初学者容易出现问题的地方.本帖通过几个实例演示matlab矩阵转置的操作. 方法一:' 运算符与 .' 运算符 >>a ...
- C#添加资源的两种方式
1.粘贴到项目Properties中的Resources.resx中 base.m_bitmap = Properties.Resources.MeasuredisTool; 2.添加已有资源中的bm ...
- 正则表达式中Pattern类、Matcher类和matches()方法简析
1.简介: java.util.regex是一个用正则表达式所订制的模式来对字符串进行匹配工作的类库包. 它包括两个类:Pattern和Matcher . Pattern: 一个Pattern是一 ...
- asp 验证
<% dim redirectUrl,checkState checkState=0 MyArray = Array("127","Feb"," ...
- 每天一个JavaScript实例-从一个div元素删除一个段落
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- linux的文件系统及节点表
linux的文件系统及节点表 一 linux的文件系统1 我们都知道当我们安装linux时会首先给系统分区,然后我们会把分区格式化成EXT3格式的文件系统.那么在linux系统中还有没有其他的文件系 ...