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.斐波那契数的更多相关文章

  1. 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]. 即计算大斐波那契数再取模. 一开始看到大斐波那契数,就想到了矩阵 ...

  2. 斐波那契数[XDU1049]

    Problem 1049 - 斐波那契数 Time Limit: 1000MS   Memory Limit: 65536KB   Difficulty: Total Submit: 1673  Ac ...

  3. 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 …… ...

  4. Project Euler 104:Pandigital Fibonacci ends 两端为全数字的斐波那契数

    Pandigital Fibonacci ends The Fibonacci sequence is defined by the recurrence relation: F[n] = F[n-1 ...

  5. DP:斐波纳契数

    题目:输出第 n 个斐波纳契数(Fibonacci) 方法一.简单递归 这个就不说了,小n怡情,大n伤身啊……当n=40的时候,就明显感觉到卡了,不是一般的慢. //输出第n个 Fibonacci 数 ...

  6. HDU4549 M斐波那契数

    M斐波那契数列 题目分析: M斐波那契数列F[n]是一种整数数列,它的定义例如以下: F[0] = a F[1] = b F[n] = F[n-1] * F[n-2] ( n > 1 ) 如今给 ...

  7. HDU 5914 Triangle(打表——斐波那契数的应用)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5914 Problem Description Mr. Frog has n sticks, whos ...

  8. [Swift]LeetCode509. 斐波那契数 | Fibonacci Number

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

  9. HDU 1021(斐波那契数与因子3 **)

    题意是说在给定的一种满足每一项等于前两项之和的数列中,判断第 n 项的数字是否为 3 的倍数. 斐波那契数在到第四十多位的时候就会超出 int 存储范围,但是题目问的是是否为 3 的倍数,也就是模 3 ...

随机推荐

  1. java.lang.ClassNotFoundException: org.springframework.web.filter.CharacterEncodingFilter的解决方案

    白天在实验室的电脑上的项目搭起来,晚上回到宿舍发现跑不起来了,网上查到的大多不是想要的答案. 最终的解决方案是maven clean一下再重新package.

  2. jQuery中outerWidth()方法

    截图自:菜鸟教程https://www.runoob.com/jquery/html-outerwidth.html

  3. Bloom 过滤器

    待续... package com.ghc.mmall.concurrency.nio; import com.google.common.hash.BloomFilter; import com.g ...

  4. PHP5 构造函数

    在最近自己写的PHP小程序中遇到了如何使用PHP构造函数的情况,在PHP中允许我们在一个类中定义一个构造函数 如: <?php class User { public $name; functi ...

  5. Min-max theorem

    wiki链接

  6. GlobalLock锁定一个全局内存对象

    GlobalLock BAIPro

  7. 清楚webView的缓存

    +(void)clearCache{    NSHTTPCookie *cookie;    NSHTTPCookieStorage *storage = [NSHTTPCookieStorage s ...

  8. java 集合之HashMap、Hashtable、LinkedHashMap、TreeMap

    HashMap 实现了Map接口,线程不安全. 实现原理: HashMap由数组+链表组成,数组是HashMap的主体,链表则是主要为了解决哈希冲突而存在的. 如果通过hash定位到数组位置没有链表, ...

  9. Metafile::EmfToWmfBits的使用

    其中涉及到string转换LPCWSTR以及模块绝对路径的调用 #include <iostream> #include <stdio.h> #include <WIND ...

  10. 【洛谷P2292】L语言

    题目大意:给定一个长度为 N 的字符串和一个字典,字典中所有的字符串的长度均不超过 10,求给定的字符串从前往后最多有多少位可以与字典匹配. 题解:设 \(dp[i]\) 表示串的前 i 位是否能够与 ...