题目链接

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:分析:dp[i]为爬到第i个台阶需要的步数,那么dp[i] = dp[i-1] + dp[i-2], 很容易看出来这是斐波那契数列的公式                                        本文地址

class Solution {
public:
int climbStairs(int n) {
int fbn1 = 0, fbn2 = 1;
for(int i = 1; i <= n; i++)
{
int tmp = fbn1 + fbn2;
fbn1 = fbn2;
fbn2 = tmp;
}
return fbn2;
}
};

算法2:还可以根据斐波那契数列的通项公式来求,对于斐波那契数列 1 1 2 3 5 8 13 21,通项公式如下,这个方法有个缺陷是:使用了浮点数,但是浮点数精度有限, oj中n应该不大,所以可以通过(当 N>93 时 第N个数的值超过64位无符号整数可表示的范围)

具体推导请参考百度百科

class Solution {
public:
int climbStairs(int n) {
//根据斐波那契数列的通项公式
double a = 1/sqrt(5);
double b = (1 + sqrt(5)) / 2;
double c = (1 - sqrt(5)) / 2;
return (int)round(a * (pow(b, n+1) - pow(c, n+1)));
}
};

算法3:”编程之美2.9-斐波那契数列“ 中提到了一种logn的算法(实际上利用了幂运算的logn算法),在n比较大时,会高效很多。首先给出本题代码,然后直接截图书上的描述。如果n较大,就需要编写大整数类了

     struct matrix22
{
int v11,v12,v21,v22;
matrix22(int a,int b,int c,int d)
{
v11 = a; v12 = b; v21 = c; v22 = d;
}
matrix22(){}
};
matrix22 matMult(const matrix22 &a, const matrix22 &b)//矩阵乘法
{
matrix22 res;
res.v11 = a.v11*b.v11 + a.v12*b.v21;
res.v12 = a.v11*b.v12 + a.v12*b.v22;
res.v21 = a.v21*b.v11 + a.v22*b.v21;
res.v22 = a.v21*b.v12 + a.v22*b.v22;
return res;
}
matrix22 matPow(const matrix22 &a, int exp)//矩阵求幂
{
matrix22 res(,,,);//初始化结果为单位矩阵
matrix22 tmp = a;
for(; exp; exp >>= )
{
if(exp & )
res = matMult(res, tmp);
tmp = matMult(tmp, tmp);
}
return res;
} class Solution {
public:
int climbStairs(int n) {
matrix22 A(,,,);
A = matPow(A, n-);
return A.v11 + A.v21;
}
};

【版权声明】转载请注明出处http://www.cnblogs.com/TenosDoIt/p/3465356.html

LeetCode:Climbing Stairs(编程之美2.9-斐波那契数列)的更多相关文章

  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. 【LeetCode】1414. 和为 K 的最少斐波那契数字数目 Find the Minimum Number of Fibonacci Numbers Whose Sum Is K

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 贪心 日期 题目地址:https://leetcode ...

  3. [LeetCode] 70. Climbing Stairs(斐波那契数列)

    [思路] a.因为两种跳法,1阶或者2阶,那么假定第一次跳的是一阶,那么剩下的是n-1个台阶,跳法是f(n-1); b.假定第一次跳的是2阶,那么剩下的是n-2个台阶,跳法是f(n-2) c.由a.b ...

  4. leetcode 509斐波那契数列

    递归方法: 时间O(2^n),空间O(logn) class Solution { public: int fib(int N) { ?N:fib(N-)+fib(N-); } }; 递归+记忆化搜索 ...

  5. 【LeetCode每天一题】Fibonacci Number(斐波那契数列)

    The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such th ...

  6. [每日一题2020.06.14]leetcode #70 爬楼梯 斐波那契数列 记忆化搜索 递推通项公式

    题目链接 题意 : 求斐波那契数列第n项 很简单一道题, 写它是因为想水一篇博客 勾起了我的回忆 首先, 求斐波那契数列, 一定 不 要 用 递归 ! 依稀记得当年校赛, 我在第一题交了20发超时, ...

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

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

  8. LeetCode——Climbing Stairs

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

  9. [LeetCode] Split Array into Fibonacci Sequence 分割数组成斐波那契序列

    Given a string S of digits, such as S = "123456579", we can split it into a Fibonacci-like ...

随机推荐

  1. C++语言出现的bug

    输出语句不管是C语言的printf();还是cout << "" << endl; 在循环语句中会出现一个bug: 下面是不正常的两种情况: 下面是正常的: ...

  2. Android之 左右滑动菜单

    近来学习了极客学院有关于界面左右滑动的视频,就在这里写个博客,巩固一下知识点,以免忘了. 这里主要介绍界面是如何左右滑动的: 1.首先我们应该设置好将要滑动的三个界面,即leftMenu.middle ...

  3. C# 3个线程A B C 依次打印123123123..

    C#经典面试题: 有3个线程,A线程打印1,B线程打印2,C线程打印3,请用程序实现依次打印123123123... class Program { static void Main(string[] ...

  4. redis中set命令的源码分析

    首先在源码中的redis.c文件中有一个结构体:redisCommand redisCommandTable[],这个结构体中定义了每个命令对应的函数,源码中的set命令对应的函数是setComman ...

  5. PHP开发第一个扩展

    首先声明:我们要构建的是扩展或者模块名为hello_module.该模块提供一个方法:hello_word. 一.PHP环境的搭建 1)一般使用源码包编译安装,而不是binary包安装.因为使用PHP ...

  6. try catch finally 用法

    trycatchfinally 1.将预见可能引发异常的代码包含在try语句块中.2.如果发生了异常,则转入catch的执行.catch有几种写法:catch这将捕获任何发生的异常.catch(Exc ...

  7. 前端mock数据之mockjax和mockjson

    用处 在前后台共同进行一个项目的时候常会遇到一种情景, 后台定义好接口,前端按照接口进行开发, 当前端开发完成后台接口却还没有开发完成, 这个时候要进行接口测试, 只能等后台开发完成才能测试, 在这中 ...

  8. linux安装hadoop 1.2.1

    我的服务器里面会装很多东西,所以我在跟目录下面建立了个doc文档文件夹 1.创建存放软件的doc文件夹 mkdir doc 2.进去doc文件夹进行下载hadoop-1.2.1资源包或者到我的百度云下 ...

  9. jQuery选择器简单例子

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="jQuery_5.aspx. ...

  10. Java实现事件机制

    java中的事件机制的参与者有3种角色: 1.event object:事件状态对象,用于listener的相应的方法之中,作为参数,一般存在与listerner的方法之中 2.event sourc ...