LeetCode:Climbing Stairs(编程之美2.9-斐波那契数列)
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-斐波那契数列)的更多相关文章
- [LeetCode] Climbing Stairs 斐波那契数列
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- 【LeetCode】1414. 和为 K 的最少斐波那契数字数目 Find the Minimum Number of Fibonacci Numbers Whose Sum Is K
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 贪心 日期 题目地址:https://leetcode ...
- [LeetCode] 70. Climbing Stairs(斐波那契数列)
[思路] a.因为两种跳法,1阶或者2阶,那么假定第一次跳的是一阶,那么剩下的是n-1个台阶,跳法是f(n-1); b.假定第一次跳的是2阶,那么剩下的是n-2个台阶,跳法是f(n-2) c.由a.b ...
- leetcode 509斐波那契数列
递归方法: 时间O(2^n),空间O(logn) class Solution { public: int fib(int N) { ?N:fib(N-)+fib(N-); } }; 递归+记忆化搜索 ...
- 【LeetCode每天一题】Fibonacci Number(斐波那契数列)
The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such th ...
- [每日一题2020.06.14]leetcode #70 爬楼梯 斐波那契数列 记忆化搜索 递推通项公式
题目链接 题意 : 求斐波那契数列第n项 很简单一道题, 写它是因为想水一篇博客 勾起了我的回忆 首先, 求斐波那契数列, 一定 不 要 用 递归 ! 依稀记得当年校赛, 我在第一题交了20发超时, ...
- [LeetCode] 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
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- [LeetCode] Split Array into Fibonacci Sequence 分割数组成斐波那契序列
Given a string S of digits, such as S = "123456579", we can split it into a Fibonacci-like ...
随机推荐
- Windows Server 2008 R2怎样设置自动登陆
Windows Server 2008 R2是一款服务器操作系统,提升了虚拟化.系统管理弹性.网络存取方式,以及信息安全等领域的应用,Windows Server 2008 R2也是第一个只提供64位 ...
- Ubuntu文本编辑时vi和nano命令的区别(建议使用nano)
vi是Unix世界里极为普遍的全荧幕文书编辑器,几乎可以说任何一台Unix机器都会提供这套软体就像windows的记事本一样. 键入 vi /etc/hosts 进入vi界面,把光标移动到文件未尾.按 ...
- 一次简单的MySQL数据库导入备份
任务目的:把现网数据库(MySQL5.5,windows)中的内容导入到测试数据库(MySQL5.1,linux)中 1.由于对MySQL并不熟悉,一上来我先考虑方案是用现成的数据库管理工具来处理.我 ...
- 从零开始学node(一): nodejs开发环境的配置
从零开始学node系列(一): nodejs环境安装 一.安装node.js 1. node官网,node安装十分方便快捷,所以这一步还是很顺利的. 2. webstorm是一款强大的前端开发IDE, ...
- BaseDao
public class BaseDao { private static Log logger = LogFactory.getLog(BaseDao.class); // 查询数据 public ...
- 【VB超简单入门】二、知识准备
在开始编程之前,需要先熟悉一下各种操作和术语,以后学习编程才能得心应手. 首先最重要的操作当然就是-电脑的开机关机啦~(开个玩笑哈哈),必须掌握软件的安装和卸载,还有能编写批处理程序对平时的使用也是很 ...
- Python基本语法初试
编程环境: win7旗舰版 Python 3.2.2(default, Sep 4 2011,09:51:08) 代码来源:(Python菜鸟) 代码内容: Python基本的输出语句print(& ...
- 用SecureCRT在windows和CentOS间上传下载文件
安装lrzsz: # yum -y install lrzsz 现在就可以正常使用rz.sz命令上传.下载数据了 配置SecureCRT的session选项的SFTP标签页和X/Y/Zmodem中的目 ...
- OpenXml入门----给Word文档添加文字
使用OpenXml给word文档添加文字,每个模块都有自己对于的属性以及内容,要设置样式就先声明属性对象,将样式Append到属性里面,再将属性append到模块里面,那么模块里面的内容就具备该样式了 ...
- 设计模式笔记感悟 - Creational篇
body,td,p { // 这对大括号里描述网页的背景 margin-left:40px; margin-right:40px; font-size: 10pt; } div.vim { width ...