10- I. 斐波那契数列

链接

https://leetcode-cn.com/problems/fei-bo-na-qi-shu-lie-lcof/
难度: #简单

题目

写一个函数,输入 n ,求斐波那契(Fibonacci)数列的第 n 项(即 F(N))。斐波那契数列的定义如下:

F(0) = 0,   F(1) = 1
F(N) = F(N - 1) + F(N - 2), 其中 N > 1.

斐波那契数列由 0 和 1 开始,之后的斐波那契数就是由之前的两数相加而得出。
答案需要取模 1e9+7(1000000007),如计算初始结果为:1000000008,请返回 1。

示例 1:

输入:n = 2
输出:1

示例 2:

输入:n = 5
输出:5

提示:0 <= n <= 100

代码框架

class Solution {
    public int fib(int n) {

    }
}

题目解析

解答思路1:
递归解法,
使用备忘录缓存结果,
避免重复计算子问题导致堆栈溢出,
注意每次计算后的结果都要取模。

解答思路2:
迭代解法,
自底向上求解,
使用备忘录缓存结果。

解答思路3:
迭代解法,
使用两个临时变量记录前两步的值,
然后计算当前值了,
再更新两个临时变量用于下一次计算。

解答思路4:
纯查表法,
预先计算好结果存入数组,
然后直接查找即可,
不失为一种提升性能的方法。

测试用例

package edu.yuwen.sowrd.num10_I.solution;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import edu.yuwen.sowrd.num10_I.sol1.Solution;

public class SolutionTest {

    /**
     * 示例 1:
     * 输入:n = 2
     * 输出:1
     */
    @Test
    public void testCase1() {
        Solution solution = new Solution();

        int res = solution.fib(2);
        Assertions.assertEquals(1, res);
    }

    /**
     * 示例 2:
     * 输入:n =5
     * 输出:5
     */
    @Test
    public void testCase2() {
        Solution solution = new Solution();

        int res = solution.fib(5);
        Assertions.assertEquals(5, res);
    }
}

解答1

package edu.yuwen.sowrd.num10_I.sol1;

public class Solution {
    private static final int MOD_NUM = 1000000007;
    // 用于缓存执行结果
    private int[] cache = new int[100 + 1];

    public int fib(int n) {
        if (n == 0) {
            return 0;
        }
        if (n == 1) {
            return 1;
        }
        int first;
        if (cache[n - 1] != 0) {
            first = cache[n - 1];
        } else {
            first = fib(n - 1);
        }

        int second;
        if (cache[n - 2] != 0) {
            second = cache[n - 2];
        } else {
            second = fib(n - 2);
        }

        int res = first + second;
        res = res % MOD_NUM;
        cache[n] = res;
        return res;
    }

}

解答2

package edu.yuwen.sowrd.num10_I.sol2;

public class Solution {
    private static final int MOD_NUM = 1000000007;
    // 用于缓存执行结果
    private int[] cache = new int[100 + 1];

    public int fib(int n) {
        cache[0] = 0;
        cache[1] = 1;

        for (int i = 2; i <= n; i++) {
            cache[i] = (cache[i - 1] + cache[i - 2]) % MOD_NUM;
        }

        return cache[n];
    }

}

解答3 推荐

package edu.yuwen.sowrd.num10_I.sol3;


public class Solution {
    private static final int MOD_NUM = 1000000007;

    public int fib(int n) {
        if (n == 0 || n == 1) {
            return n;
        }

        // 使用两个变量记录前两步的值
        int first = 0;
        int second = 1;

        int current = 0;
        for (int i = 2; i <= n; i++) {
            current = (first + second) % MOD_NUM;
            // 根据新的值,更新前两步的值
            first = second;
            second = current;
        }

        return current;
    }

}

解答4

package edu.yuwen.sowrd.num10_I.sol4;

public class Solution {
    int f[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610,
            987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025,
            121393, 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578,
            5702887, 9227465, 14930352, 24157817, 39088169, 63245986, 102334155,
            165580141, 267914296, 433494437, 701408733, 134903163, 836311896,
            971215059, 807526948, 778742000, 586268941, 365010934, 951279875,
            316290802, 267570670, 583861472, 851432142, 435293607, 286725742,
            722019349, 8745084, 730764433, 739509517, 470273943, 209783453,
            680057396, 889840849, 569898238, 459739080, 29637311, 489376391,
            519013702, 8390086, 527403788, 535793874, 63197655, 598991529,
            662189184, 261180706, 923369890, 184550589, 107920472, 292471061,
            400391533, 692862594, 93254120, 786116714, 879370834, 665487541,
            544858368, 210345902, 755204270, 965550172, 720754435, 686304600,
            407059028, 93363621, 500422649, 593786270, 94208912, 687995182 };

    int fib(int n) {
        return f[n];
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值