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 ...
随机推荐
- 关于移动端click事件绑定的一个细节
click是最常见的点击事件,但是对于移动终端,比如手机,一般都是以touch事件代替的,而click事件在手机也是生效的,只是会有1-2秒左右的延迟,那么当你想要用click而非touch事件的时候 ...
- Java静态同步方法和非静态同步方法
所有的非静态同步方法用的都是同一把锁——该实例对象本身.也就是说如果一个实例对象的非静态同步方法获取锁后,该实例对象的其他非静态同步方法必须等待获取锁的方法释放锁后才能获取锁进而执行 ...
- MFC Grid control 2.27
原文链接地址:http://www.codeproject.com/Articles/8/MFC-Grid-control MFCGridCtrl是个强大的类,用于数据的表格显示. 1.类特征 Cel ...
- windows 搭建 solr 5.3.2
1. Tamcat 的安装,此不介绍 路径:F:\SolrTest\apache-tomcat-8.0.18 2. 解压 solr 5.3.2 路径:F:\Tool\solr-5.3.2 3. 复制s ...
- EXECL文件导入数据库
Execl数据导入数据库: 注意事项:execl中的列名与列数要与数据库的列名和列数一致.值类型一致,列名不一致的话可在导入的时候,给字段起别名,确定保持一致 v 界面代码: <div> ...
- 第1章 Java中常用字符串方法总结
1.1 charAt方法——提取指定字符 1.2 codePointAt方法——提取索引字符代码点 1.3 codePointBefore方法——获取索引前一个字符的代码点 1.4 codePoint ...
- C++杂谈(一)const限定符与const指针
const限定符 c++有了新的const关键字,用来定义常变量,可以替C语言中的#define.关于const限定符,有以下需要注意: 1.创建后值不再改变 2.作用范围在文件内有效 3.添加ext ...
- fcntl 获取文件状态标志
int fcntl(int fd,int cmd,...) 函数fcntl提供了非常丰富的功能.主要依赖于cmd的各种参数: 复制已有的文件描述符 F_DUPFD,F_DUPFD_CLOEXEC 获取 ...
- 开创学习的四核时代-迅为iTOP4412学习开发板
产品特点: 处理器: Exynos 4412 处理器,Cortex-A9四核,功耗性能俱佳! 性能: 1GB(可选2GB) 双通道 64bit数据总线 DDR3: 4GB(可选16GB)固态硬盘EMM ...
- Web安全--XSS模版
[XSS基本探测pyload] <script>alert("xss")</script> <script>alert(/xss/)< ...