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 ...
随机推荐
- Installation and Upgrading
Cumulative Feature Overview Identifies product features available to you when upgrading. This tool r ...
- Winform开发框架之插件化应用框架实现
支持插件化应用的开发框架能给程序带来无穷的生命力,也是目前很多系统.程序追求的重要方向之一,插件化的模块,在遵循一定的接口标准的基础上,可以实现快速集成,也就是所谓的热插拔操作,可以无限对已经开发好系 ...
- Chrome弹窗的简单应用(选择结构与循环结构)
★选择结构★ ★JS实现弹窗显示随机数 示例代码效果图 ★ 弹窗实现对随机数的进一步判断 示例代码效果图 ★综合应用 比较大小 ★ 判断成绩等级 ): : : : : alert(" ...
- 一个端口操作类PortHelper
此操作类主要使用了系统命令行来实现的,详细的命令可参照微软文档<如何使用"netsh advfirewall firewall"上下文而非“netsh firewall”上下 ...
- java 接口学习
你应该知道接口是一种契约,它与实现方式无关 但是类,即使是抽象类,你都能自定义成员变量,而成员变量往往就与实现方式有关. 这一点的实际意义不大. 但是有一点,类会暴露太多不必要,甚至不能暴露的东西,你 ...
- PHP验证邮箱地址代码
PHP验证邮箱代码: function isEmail($email) { return strlen($email) > 6 && preg_match("/^[\w ...
- 关于IIS寄宿WCF服务,客户端不能生成代理类
我在使用VS2010写好WCF的Web服务后,部署在IIS7.0上,可以在IE9上进行访问,并且能显示XML数据,如下图 然后我在项目的客户端进行服务的添加引用,如下图 VS2010自动生成代理类,但 ...
- GWYAlertSelectView 选择收货地址和选择联系人
iOS 开发在app内(特别是在类似购物和旅游类)经常会用到选择,添加联系人和收货地址,而且经常都是在跳转界面,很是麻烦,今天我就自己封装了一个类似弹出视图的代码,方便了很多!demo地址: http ...
- dapper的增、删、查改的CodeSmith模板
<%@ Template Language="C#" TargetLanguage="Text" %> <%@ Property Name=& ...
- 控制台(Console)报错:java.io.IOException: Broken pipe
控制台(Console)输出: java.io.IOException: Broken pipe at sun.nio.ch.FileDispatcherImpl.write0(Native Meth ...