LeetCode_509.斐波那契数
LeetCode-cn_509
斐波那契数,通常用 F(n) 表示,形成的序列称为斐波那契数列。该数列由 0 和 1 开始,后面的每一项数字都是前面两项数字的和。也就是:
F(0) = 0, F(1) = 1
F(N) = F(N - 1) + F(N - 2), 其中 N > 1.
给定 N,计算 F(N)。
示例 1:
输入:2
输出:1
解释:F(2) = F(1) + F(0) = 1 + 0 = 1.
示例 2:
输入:3
输出:2
解释:F(3) = F(2) + F(1) = 1 + 1 = 2.
示例 3:
输入:4
输出:3
解释:F(4) = F(3) + F(2) = 2 + 1 = 3.
提示:
0 ≤ N ≤ 30
方法一:递归方法题解
- 测试用例:31(0~30)
- 执行用时:18ms
- 内存消耗:33.4MB
class Solution {
public int fib(int N) {
if (N < 2) return N;
return fib(N-1) + fib(N-2);
}
}
方法二:动态编程方法题解
- 测试用例:31(0~30)
- 执行用时:1ms
- 内存消耗:33.8MB
class Solution {
public int fib(int N) {
if (N < 2) return N;
int[] fib = new int[N+1];
fib[0] = 0;
fib[1] = 1;
for (int i = 2; i <= N; i++) {
fib[i] = fib[i-1] + fib[i-2];
}
return fib[N];
}
}
方法三:矩阵幂方法题解
- 测试用例:31(0~30)
- 执行用时:1ms
- 内存消耗:33.5MB
class Solution {
public int fib(int N) {
if (N < 2) return N;
int F[][] = new int[][] { { 1, 1 }, { 1, 0 } };
power(F, N - 1);
return F[0][0];
}
void power(int F[][], int N) {
int M[][] = new int[][] { { 1, 1 }, { 1, 0 } };
for (int i = 2; i <= N; i++) {
mutiply(F, M);
}
}
void mutiply(int F[][], int M[][]) {
int x = F[0][0] * M[0][0] + F[0][1] * M[1][0];
int y = F[0][0] * M[0][1] + F[0][1] * M[1][1];
int z = F[1][0] * M[0][0] + F[1][1] * M[1][0];
int w = F[1][0] * M[0][1] + F[1][1] * M[1][1];
F[0][0] = x;
F[0][1] = y;
F[1][0] = z;
F[1][1] = w;
}
}
方法四:优化空间复杂度为 O(1) 题解
- 测试用例:31(0~30)
- 执行用时:0ms
- 内存消耗:33.3MB
class Solution {
public int fib(int N) {
if (N < 2) return N;
int a = 0, b = 1, c;
for (int i = 2; i <= N; i++) {
c = a + b;
a = b;
b = c;
}
return b;
}
}
LeetCode_509.斐波那契数的更多相关文章
- UVA 11582 Colossal Fibonacci Numbers! 大斐波那契数
大致题意:输入两个非负整数a,b和正整数n.计算f(a^b)%n.其中f[0]=f[1]=1, f[i+2]=f[i+1]+f[i]. 即计算大斐波那契数再取模. 一开始看到大斐波那契数,就想到了矩阵 ...
- 斐波那契数[XDU1049]
Problem 1049 - 斐波那契数 Time Limit: 1000MS Memory Limit: 65536KB Difficulty: Total Submit: 1673 Ac ...
- C++求斐波那契数
题目内容:斐波那契数定义为:f(0)=0,f(1)=1,f(n)=f(n-1)+f(n-2)(n>1且n为整数) 如果写出菲氏数列,则应该是: 0 1 1 2 3 5 8 13 21 34 …… ...
- Project Euler 104:Pandigital Fibonacci ends 两端为全数字的斐波那契数
Pandigital Fibonacci ends The Fibonacci sequence is defined by the recurrence relation: F[n] = F[n-1 ...
- DP:斐波纳契数
题目:输出第 n 个斐波纳契数(Fibonacci) 方法一.简单递归 这个就不说了,小n怡情,大n伤身啊……当n=40的时候,就明显感觉到卡了,不是一般的慢. //输出第n个 Fibonacci 数 ...
- HDU4549 M斐波那契数
M斐波那契数列 题目分析: M斐波那契数列F[n]是一种整数数列,它的定义例如以下: F[0] = a F[1] = b F[n] = F[n-1] * F[n-2] ( n > 1 ) 如今给 ...
- HDU 5914 Triangle(打表——斐波那契数的应用)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5914 Problem Description Mr. Frog has n sticks, whos ...
- [Swift]LeetCode509. 斐波那契数 | Fibonacci Number
The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such th ...
- HDU 1021(斐波那契数与因子3 **)
题意是说在给定的一种满足每一项等于前两项之和的数列中,判断第 n 项的数字是否为 3 的倍数. 斐波那契数在到第四十多位的时候就会超出 int 存储范围,但是题目问的是是否为 3 的倍数,也就是模 3 ...
随机推荐
- IDEA怎么关闭暂时不用的工程
一.隐藏 二.隐藏之后显示显示模块 原文地址:https://blog.csdn.net/woshilovetg/article/details/82774437
- 《一头扎进》系列之Python+Selenium自动化测试框架实战篇6 - 价值好几K的框架,呦!这个框架还真牛叉哦!!!
1. 简介 本文开始介绍如何通过unittest来管理和执行测试用例,这一篇主要是介绍unittest下addTest()方法来加载测试用例到测试套件中去.用addTest()方法来加载我们测试用例到 ...
- 在springboot中集成jsp开发
springboot就是一个升级版的spring.它可以极大的简化xml配置文件,可以采用全注解形式开发,一个字就是很牛.在springboot想要使用jsp开发,需要集成jsp,在springboo ...
- css的样式问题
项目里面遇到一个布局: 然后侧边栏菜单的高度要随着内容的高度变化而变化:所以在这里贴一下代码:效果如下 <!DOCTYPE html> <html lang="en&quo ...
- 十三、S3C2440 裸机 — 初始化代码及MMU
13.1 NOR FLASH 搬运 把程序从 nor flash 上搬运到 SDRAM 中 程序存储在 nor flash 上,运行时将程序搬运到 SDRAM 中运行 nor flash 启动:nor ...
- [ZJOI2014]力 题解
题目地址 洛谷P3338 Solution 第一道FFT的应用AC祭! 我们要求: \[E_j=\frac{F_j}{q_j}=\sum_{i<j}\frac{q_i}{(i-j)^2}-\su ...
- Spring mvc 搭建Mybatis
本文建立在spring mvc已经搭建起来的基础上. 首先看要引入的jar包,其中高亮的是为了mybatis新引入的. <properties> <spring.webm ...
- canvas 计算文字宽度(常用于文字换行)
var c=document.getElementById("myCanvas"); var ctx=c.getContext("2d"); ctx.font= ...
- CodeForces-437C(贪心)
链接: https://vjudge.net/problem/CodeForces-437C 题意: On Children's Day, the child got a toy from Delay ...
- C++ 遍历循环表达示 auto, auto&, auto&&
for(auto x : range) 创建拷贝,无法修改range中的元素 for(auto& x : range) 可以修改range中的元素,但一般用以下这种 for(auto& ...