【LeetCode练习题】Climbing Stairs
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步到达楼梯的顶部。每一次你只能上一个或两级台阶,问要到达顶部一共有多少种方法?
解题思路:
真是太巧了!!我今天刚刚在《剑指offer》里读到了一模一样的原题,在该书的75页。简单介绍一下吧:
如果只有一级台阶,那显然只有一种方法。如果有2级台阶,那就有两种上的方法了:一种是分两次,每次上一级;另外一种就是一次上2级。
接着讨论一般情况,我们把n级台阶时的方法数看成是n的函数,记为f(n)。当n>2时,第一次上的时候就有两种不同的选择:一是第一次只登一级,此时登法数目等于后面剩下的n-1级台阶的登法数目,即f(n-1);另外一种选择是第一次上2级,此时方法数目等于后面剩下的n-2级台阶的方法数目,即f(n-2)。因此n级台阶的不同登法总数等于f(n-1)+f(n-2)。不难看出这实际上就是斐波那契数列了。
注:因为斐波那契解法大都采用递归,实际上递归的解法存在很严重的效率问题,有大量的重复计算。当要计算的数很大时,速度极慢且有可能出现函数调用栈溢出的情况。所以实用的解法是采用循环,时间复杂度是O(n)。
代码如下:
class Solution {
public:
int climbStairs(int n) {
return Fibonacci(n);
}
int Fibonacci(int n){
int result[] = {,};
if(n < ){
return result[n-];
}
int NminusOne = ;
int NminusTwo = ;
int N = ;
for(int i = ; i <= n; i++){
N = NminusOne + NminusTwo;
NminusTwo = NminusOne;
NminusOne = N;
}
return N;
}
};
【LeetCode练习题】Climbing Stairs的更多相关文章
- [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 OJ]-Climbing Stairs
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- [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 ...
- 【leetcode】Climbing Stairs
题目简述: You are climbing a stair case. It takes n steps to reach to the top. Each time you can either ...
随机推荐
- 关于oracle动态视图v$datafile和v$datafile_header(转)
v$datafile是从oracle的控制文件中获得的数据文件的信息v$datafile_header是从数据文件的头部在正常运行下,两者的检查点SCN值是一致的,但当datafile出现损坏时可以用 ...
- The type R is already defined 错误解决办法
今天在导入一个开源项目的时候遇到了The type R is already defined的错误,试过了删除R,clear project都还是报这个错,Google一下之后找到了解决办法在 Pro ...
- Combination Sum II 解答
Question Given a collection of candidate numbers (C) and a target number (T), find all unique combin ...
- Pascal's Triangle 解答
Question Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows ...
- poj 3411 Paid Roads(dfs)
Description A network of m roads connects N cities (numbered to N). There may be more than one road ...
- C#常用語法糖(Csharp Syntactic sugar)
首先需要声明的是“语法糖”这个词绝非贬义词,它可以给我带来方便,是一种便捷的写法,编译器会帮我们做转换:而且可以提高开发编码的效率,在性能上也不会带来损失.这让java开发人员羡慕不已,呵呵. 1. ...
- 使用CSS达到阴阳八卦图等图形
CSS还是比較强大的,能够实现中国古典的"阴阳八卦图"等形状. 正方形 #rectangle { width: 200px; height: 100px; backgrount-c ...
- mysql1主多从配置
mysql一主多从的配置: 其实1主多从的配置与一主一从配置非常相似,现在主要讲讲一主多从的大概配置方法. 一, 1,master端开启binlog日志,并且设置server id=1. 2.重启服务 ...
- Andriod定时任务
参考地址:http://blog.sina.com.cn/s/blog_73288dd10101m6xs.html,http://blog.csdn.net/fancsxx/article/detai ...
- UITabBarController+微博简单模拟1
UITabBarController是IOS中很常用的一个viewController.UITabBarController通常作为整个程序的rootViewController,而且不能添加到别的c ...