Climbing Stairs
Climbing Stairs
https://leetcode.com/problems/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?
算法描述
(1)这道题其实是一道fibonacci数列题。
当n=1时,f(n)=1;当n=2时,f(n)=2(有1+1,2这两种方式);当n>=3时,有f(n)=f(n-1)+f(n-2)
(2)但是实现时如果直接使用递归,就会超时,所以这里使用循环
(3)用f0来记录f(n-2),用f1来记录f(n-1),按照之前的公式f(n)=f(n-1)+f(n-2),即得f2=f0+f1;然后更新f0和f1,使得这两个记录都往前移动一位,即f0=f1,f1=f2;
一共进行n-1次,返回f2
程序代码
public class Solution {
public int climbStairs(int n) {
int f0 = 1;
int f1 = 1;
int f2 = n;
n--;
while (n-- > 0) {
f2 = f0 + f1;
f0 = f1;
f1 = f2;
}
return f2;
}
}
算法2-尾递归
对于这道题,记得之前有本书提到过尾递归的思想,所以这里应用了一把尾递归,不过单就这道题看来,其实就跟循环的中心思想是一样的,都是记录做过的两个状态。
程序代码2
public class Solution {
public int climbStairsTail(int n, int acc, int acc2) {
if (n == 0) {
return acc;
}
return climbStairsTail(n - 1, acc2, acc + acc2);
}
public int climbStairs(int n) {
if (n == 0 || n == 1) {
return n;
}
return climbStairsTail(n, 1, 1);
}
}
Climbing Stairs的更多相关文章
- [LeetCode] Climbing Stairs 爬梯子问题
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- [LintCode] Climbing Stairs 爬梯子问题
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- Leetcode: climbing stairs
July 28, 2015 Problem statement: You are climbing a stair case. It takes n steps to reach to the top ...
- 54. Search a 2D Matrix && Climbing Stairs (Easy)
Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This m ...
- 3月3日(6) Climbing Stairs
原题 Climbing Stairs 求斐波那契数列的第N项,开始想用通项公式求解,其实一个O(n)就搞定了. class Solution { public: int climbStairs(int ...
- leetCode 70.Climbing Stairs (爬楼梯) 解题思路和方法
Climbing Stairs You are climbing a stair case. It takes n steps to reach to the top. Each time you ...
- 【LeetCode练习题】Climbing Stairs
Climbing Stairs You are climbing a stair case. It takes n steps to reach to the top. Each time you c ...
- 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] Min Cost Climbing Stairs 爬楼梯的最小损失
On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once you pay ...
随机推荐
- 利用代码生成工具Database2Sharp设计数据编辑界面
在Winform程序开发中,界面部分的开发工作量一般是比较大的,特别是表的字段数据比较多的情况下,数据编辑界面所需要的繁琐设计和后台逻辑处理工作量更是直线上升,而且稍不注意,可能很多处理有重复或者错误 ...
- C#全角转换成半角并检查
新建一个项目quanbanjiao,在此项目下新建一个类Class1 using System; using System.Web; using System.Text; namespace quan ...
- Python入门版
一.前言 陆陆续续学习Python已经近半年时间了,感觉到Python的强大之外,也深刻体会到Python的艺术.哲学.曾经的约定,到现在才兑现,其中不乏有很多懈怠,狼狈. Python入门关于Pyt ...
- IDEA默认常用快捷键
作为Java的利器,IDEA属实是非常好用,参考网文总结其常用快捷键如下: Ctrl + /(Ctrl + Shift + /):注释或反注释指定的语句.这个是本人最喜欢的,所以排在第一位. Ctrl ...
- Convert string to binary and binary to string in C#
String to binary method: public static string StringToBinary(string data) { StringBuilder sb = new S ...
- Error writing file‘frm‘(Errcode: 28)
Error writing file‘frm‘(Errcode: 28) mysql出现这个错误,表示磁盘已经满了,该增加容量了.
- abap 选择屏幕事件AT SELECTION-SCREEN
AT SELECTION-SCREEN (1).其实就像一个FORM,所以在这个事件里声明的变量都是局部变量. (2).根据SY-UCOMM这个系统变量可以判断用户的命令 (3).在这个事件里响应的是 ...
- 关于oracle-12514错误的修改方法
原因1: 打开文件"<OracleHome>/network/admin/listener.ora",添加 (SID_DESC = (GLOBAL_DB ...
- linux网络流量实时监控工具之iptraf
这个工具还是很强大 linux网络流量实时监控工具之iptraf [我的Linux,让Linux更易用]IPTraf是一个网络监控工具,功能比nload更强大,可以监控所有的流量,IP流量,按协议分的 ...
- android 浏览器开发实例
android app需要通过手机显示网页信息还是比较常用的,比如我最近业余开发的 抢商铺游戏,需要对游戏规则做说明,规则会比较多,而且要经常变动,就想到用网页来展示,更新起来方便,不像应用,一旦发布 ...