题目链接

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], 很容易看出来这是斐波那契数列的公式                                        本文地址

  1. class Solution {
  2. public:
  3. int climbStairs(int n) {
  4. int fbn1 = 0, fbn2 = 1;
  5. for(int i = 1; i <= n; i++)
  6. {
  7. int tmp = fbn1 + fbn2;
  8. fbn1 = fbn2;
  9. fbn2 = tmp;
  10. }
  11. return fbn2;
  12. }
  13. };

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

具体推导请参考百度百科

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

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

  1. struct matrix22
  2. {
  3. int v11,v12,v21,v22;
  4. matrix22(int a,int b,int c,int d)
  5. {
  6. v11 = a; v12 = b; v21 = c; v22 = d;
  7. }
  8. matrix22(){}
  9. };
  10. matrix22 matMult(const matrix22 &a, const matrix22 &b)//矩阵乘法
  11. {
  12. matrix22 res;
  13. res.v11 = a.v11*b.v11 + a.v12*b.v21;
  14. res.v12 = a.v11*b.v12 + a.v12*b.v22;
  15. res.v21 = a.v21*b.v11 + a.v22*b.v21;
  16. res.v22 = a.v21*b.v12 + a.v22*b.v22;
  17. return res;
  18. }
  19. matrix22 matPow(const matrix22 &a, int exp)//矩阵求幂
  20. {
  21. matrix22 res(,,,);//初始化结果为单位矩阵
  22. matrix22 tmp = a;
  23. for(; exp; exp >>= )
  24. {
  25. if(exp & )
  26. res = matMult(res, tmp);
  27. tmp = matMult(tmp, tmp);
  28. }
  29. return res;
  30. }
  31.  
  32. class Solution {
  33. public:
  34. int climbStairs(int n) {
  35. matrix22 A(,,,);
  36. A = matPow(A, n-);
  37. return A.v11 + A.v21;
  38. }
  39. };

【版权声明】转载请注明出处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. windows log 打印语句

    1.格式化字符串(Writes formatted data to the specified string) wchar_t szMessage[260]; PWSTR pszFunction = ...

  2. 管理故事——和尚挑水的故事

    有时候企业.公司的各种混乱都是源于管理问题,例如人浮于事.资源错配.机构臃肿-----,暂且不说企业管理.项目的管理,光是个人工作的管理.一个处理不好,接踵而来的就是一堆问题,可怕的不是出现问题,而是 ...

  3. SeekableByteChannel 和 FileChannel

    Java7中引入了SeekableByteChannel接口,允许我们定位到文件的任意位置进行读写.注意这里的写,不是新增式的插入,而是覆盖,当然在文件末尾的写,是新增. java.nio.chann ...

  4. JavaScript 题目破解过程与解析

    题目来源 https://www.hackthissite.org/missions/javascript/ HackThisSite JavaScript mission 1-7 1 我先尝试输入  ...

  5. .net framework 4.6.2 下载

    .net framework   .net framework版本: 4.6.2 File Name: NDP462-KB3151800-x86-x64-AllOS-ENU.exe 发布日期: 201 ...

  6. excel if判断时间段早晚班

    =IF(OR(HOUR(B3)={9,10,11,12,13,14,15,16,17,18}),"早班","晚班")

  7. uva 839 Not so Mobile-S.B.S.

    Before being an ubiquous communications gadget, a mobilewas just a structure made of strings and wir ...

  8. 边工作边刷题:70天一遍leetcode: day 84-2

    要点:这题是combination的应用,从左向右想比从右向左容易. 因为有结果从小到大的要求,暗示用combintion而不是permutation 其实就是从小到大验证因子,每个因子和其对称因子立 ...

  9. [转载]ExtJs4 笔记(8) Ext.slider 滚轴控件、 Ext.ProgressBar 进度条控件、 Ext.Editor 编辑控件

    作者:李盼(Lipan)出处:[Lipan] (http://www.cnblogs.com/lipan/)版权声明:本文的版权归作者与博客园共有.转载时须注明本文的详细链接,否则作者将保留追究其法律 ...

  10. AC日记——信息传递 洛谷 P2661 (tarjan求环)

    题目描述 有n个同学(编号为1到n)正在玩一个信息传递的游戏.在游戏里每人都有一个固定的信息传递对象,其中,编号为i的同学的信息传递对象是编号为Ti同学. 游戏开始时,每人都只知道自己的生日.之后每一 ...