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的更多相关文章

  1. [LeetCode] Climbing Stairs 爬梯子问题

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...

  2. [LintCode] Climbing Stairs 爬梯子问题

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...

  3. Leetcode: climbing stairs

    July 28, 2015 Problem statement: You are climbing a stair case. It takes n steps to reach to the top ...

  4. 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 ...

  5. 3月3日(6) Climbing Stairs

    原题 Climbing Stairs 求斐波那契数列的第N项,开始想用通项公式求解,其实一个O(n)就搞定了. class Solution { public: int climbStairs(int ...

  6. leetCode 70.Climbing Stairs (爬楼梯) 解题思路和方法

    Climbing Stairs  You are climbing a stair case. It takes n steps to reach to the top. Each time you ...

  7. 【LeetCode练习题】Climbing Stairs

    Climbing Stairs You are climbing a stair case. It takes n steps to reach to the top. Each time you c ...

  8. 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 ...

  9. [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 ...

随机推荐

  1. SQL Server 多条记录的某个字段拼接

    USE [FM_Dev] GO /****** 对象: UserDefinedFunction [dbo].[GetClassNameByStudentCode] 脚本日期: 05/23/2014 1 ...

  2. asp.net 网页抓取内容

    网页抓取代码 using System; using System.Collections.Generic; using System.Linq; using System.Web; // using ...

  3. 【C#进阶系列】10 属性

    属性分为无参属性和有参属性(即索引器). 属性相对于字段的优点不仅仅是为了封装,还可以在读写的时候做一些额外操作,缓存某些值或者推迟创建一些内部对象,也适用于以线程安全的方式访问字段. 话说最基本的属 ...

  4. 重新想象 Windows 8.1 Store Apps (76) - 新增控件: SearchBox

    [源码下载] 重新想象 Windows 8.1 Store Apps (76) - 新增控件: SearchBox 作者:webabcd 介绍重新想象 Windows 8.1 Store Apps 之 ...

  5. 防止用户误操作退出APP的处理

    /** * 软件退出的处理:先跳到第一个页面,再点提示“再点一次退出”,2秒内再点一次退出 * 防止用户误操作 */ private boolean isExist=false; private Ha ...

  6. jdk1.8 ThreadPoolExecutor实现机制分析

    ThreadPoolExecutor几个重要的状态码字段 private static final int COUNT_BITS = Integer.SIZE - 3; private static ...

  7. 从自签名证书导出pfx和cer证书

    完整代码: public sealed class DataCertificate { #region 生成证书 /// <summary> /// 根据指定的证书名和makecert全路 ...

  8. PHP程序z中xdebug工具简要使用方法

    PHP程序的debug PHP程序的debug,无论是cli方式还是web方式,都需要使用第三方的debug工具.PHP5.6之前,本身自带的debug功能,仅限于日志输出. 推荐使用免费xdebug ...

  9. JSON的三种解析方式

    一.什么是JSON? JSON是一种取代XML的数据结构,和xml相比,它更小巧但描述能力却不差,由于它的小巧所以网络传输数据将减少更多流量从而加快速度. JSON就是一串字符串 只不过元素会使用特定 ...

  10. 网站SEO之百度优化不得不知的铁人三项规则

    奥运会有铁人三项,此运动更好的协调了运动员的综合素质水平,而百度优化排名中的“铁人三项”规则则是让网站的整体质量更好的满足市场用户体验.针对不同部分的操作,可以让网站在每个细节处都能凸显以人为本的服务 ...