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. (转载)IO-同步、异步、阻塞、非阻塞

    一.概述 同步(synchronous) IO和异步(asynchronous) IO,阻塞(blocking) IO和非阻塞(non-blocking)IO分别是什么,到底有什么区别?这个问题其实不 ...

  2. c#开发工具软件集合

    visual studio 2015(自带Nuget) Resharper de4dot dnspy ILMergeGui Git 大漠插件3.1233 天使插件v4.019 Navicat_Prem ...

  3. C#实现的18位身份证格式验证算法

    18位身份证标准在国家质量技术监督局于1999年7月1日实施的GB11643-1999<公民身份号码>中做了明确的规定. GB11643-1999<公民身份号码>为GB1164 ...

  4. 怎样用C#代码屏蔽任务管理器?

    这是我在网上找的并多加了一些我自己需要的代码,经过我的测试,可以屏蔽任务管理器,但还有一些瑕疵. 首先,我在vs2012中新建一个项目,选择window下的window窗体应用程序,把窗体form1拉 ...

  5. 不可或缺 Windows Native (9) - C 语言: 动态分配内存,链表,位域

    [源码下载] 不可或缺 Windows Native (9) - C 语言: 动态分配内存,链表,位域 作者:webabcd 介绍不可或缺 Windows Native 之 C 语言 动态分配内存 链 ...

  6. 重新想象 Windows 8.1 Store Apps (78) - 控件增强: ScrollViewer, FlipView, Popup

    [源码下载] 重新想象 Windows 8.1 Store Apps (78) - 控件增强: ScrollViewer, FlipView, Popup 作者:webabcd 介绍重新想象 Wind ...

  7. JPA(5)使用二级缓存

    jpa的缓存分为一级缓存和二级缓存,一级缓存值得是会话级别的,而二级缓存是跨会话级别的. 使用二级缓存,使用到了Ehcache,首先第一步需要在配置文件中配置使用了二级缓存 <shared-ca ...

  8. 深入理解php中的ini配置(1)

    这篇文章不会详细叙述某个ini配置项的用途,这些在手册上已经讲解的面面俱到.我只是想从某个特定的角度去挖掘php的实现机制,会涉及到一些php内核方面的知识:-) 使用php的同学都知道php.ini ...

  9. mysql=null的优雅解决方法

    对于不是采用所有字段都是not null的mysql表设计而言,mysql提供了一个<=>操作符. 在oracle中我们的处理方式通常类似: where a = #{var} or #{v ...

  10. 关于setInterval和setTImeout中的this指向问题

    前些天在练习写一个小例子的时候用到了定时器,发现在setInterval和setTimeout中传入函数时,函数中的this会指向window对象,如下例: var num = 0; function ...