leetCode 70.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?
思路:题目也比較简单。类似斐波那契。
代码例如以下:
public class Solution {
public int climbStairs(int n) {
//此题相似斐波那契数列
if(n <= 0){
return 0;
}
if(n <= 1){
return 1;
}
if( n <= 2){
return 2;
}
int i = 3;
int f1 = 1;
int f2 = 2;
int f3 = 0;
while(i++ <= n){
f3 = f1+f2;
f1 = f2;
f2 = f3;
}
return f3;
}
}
leetCode 70.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爬楼梯
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爬楼梯 (C++)
题目: You are climbing a stair case. It takes n steps to reach to the top. Each time you can either cl ...
- Leetcode 70. Climbing Stairs 爬楼梯 (递归,记忆化,动态规划)
题目描述 要爬N阶楼梯,每次你可以走一阶或者两阶,问到N阶有多少种走法 测试样例 Input: 2 Output: 2 Explanation: 到第二阶有2种走法 1. 1 步 + 1 步 2. 2 ...
- 70. Climbing Stairs爬楼梯
网址:https://leetcode.com/problems/climbing-stairs/ 其实就是斐波那契数列,没什么好说的. 注意使用3个变量,而不是数组,可以节约空间. class So ...
- Leetcode#70. Climbing Stairs(爬楼梯)
题目描述 假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数. 示例 1: 输入: 2 输出: 2 解 ...
- 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 ...
- 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 ...
随机推荐
- ORA-01033 ORA-01109 ORA-01034 ORA-12514 ORA-24324 ORA-01041 ORA-01157 ORA-01110
客户数据库挂掉了 在plsql客户端使用普通账号登录时提示如下错误 因为好久没弄数据库了,慌了一小下. 接下来搜索过往的知识,回忆.在cli下输入了以下命令 sqlplus system/system ...
- 三菱plc输出指示灯不亮怎么办(转载)
三菱plc输出指示灯不亮怎么办?三菱plc输出指示灯故障 时间:2015-10-21 07:31:12编辑:电工栏目:三菱plc 导读:三菱plc输出指示灯不亮故障的原因,三菱plc在使用中出现输出指 ...
- ie8下jquery读取当前点击的标签位置错误,原因是里面有内容写了text-indent:-9999px
今天写一地图的效果,鼠标点击对应的区域,弹出所点击区域的名字. 因为设计的区域名字有特殊效果,所以,在点击区域里面套了个标签写上区域名字用来识别,但是这个文字呢不同显示在页面上,所以就给 em 加个了 ...
- BZOJ 1711: [Usaco2007 Open]Dingin吃饭
Description 农夫JOHN为牛们做了很好的食品,但是牛吃饭很挑食. 每一头牛只喜欢吃一些食品和饮料而别的一概不吃.虽然他不一定能把所有牛喂饱,他还是想让尽可能多的牛吃到他们喜欢的食品和饮料. ...
- NODE.JS玩玩
按一个网页的来,最好最后能到EXPRESS.JS. http://www.nodebeginner.org/index-zh-cn.html 这样就能对比DJANGO,看看两者的WEB框架,加深认识. ...
- web 文件上传 无刷新的方法 使用iframe
<html> <script type="text/javascript" src="admin/view/js/jquery.min.js" ...
- DHTMLX 前端框架 建立你的一个应用程序教程(四)--添加一个工具条toolbar
工具条例子 样本如下: 这里我们使用的是dhtmlxToolbar 组件. 添加工具栏到布局中: 1.使用attachToolbar() 方法初始化页面 添加代码到index.html中 dhtmlx ...
- 如何在win下编译thunderbird
最近突然想研究一下thunderbird的实现,于是在WIN2K3下对其进行了系列的编译,特将编译的一些心得与大家共享.其实编译过程已经非常简单了,本文以VC8 ( VISUAL STUDIO 200 ...
- 开始hadoop
hadoop介绍 分布式存储系统HDFS(Hadoop Distributed File System),提供了高可靠性.高扩展性和高吞吐率的数据存储服务: 资源管理系统YARN(Yet Anothe ...
- 【CF】110 Div.1 B. Suspects
这题目乍眼一看还以为是2-sat.其实很水的,O(n)就解了.枚举每个人,假设其作为凶手.观察是否满足条件.然后再对满足的数目分类讨论,进行求解. /* 156B */ #include <io ...