头图

LeetCode-509-Fibonacci Number

Fibonacci number

Title description: Fibonacci number, usually expressed by F(n), the sequence formed is called Fibonacci sequence. The number sequence starts with 0 and 1, and each number after it is the sum of the previous two numbers. That is:

  • F(0) = 0,F(1) = 1
  • F(n) = F(n-1) + F(n-2), where n> 1

Give you n, please calculate F(n).

Please refer to LeetCode official website for example description.

Source: LeetCode
Link: https://leetcode-cn.com/problems/fibonacci-number/
The copyright belongs to Lingkou Network. For commercial reprints, please contact the official authorization. For non-commercial reprints, please indicate the source.

Solution one: recursive method
When n less than 2, directly return n , when greater than 2, use the formula F(n) = F(n - 1) + F(n - 2) recursively call the current method and return.
Solution 2: Iterative method

When n less than 2, it will return to you directly. When n greater than 2, the current value is calculated by iteration. The specific process is as follows:

  • The value of the first 2 bits of the current value is lastSecond , and the value of the first 1 bit of the current value is lastOne ;
  • Then start traversing from 2 to ;
  • The specific process is lastone updated lastSecond + lastOne , lastSecond updated value before ;

Finally, the value returned by lastOne is the current value.

/**
 * @Author: ck
 * @Date: 2021/10/3 10:33 上午
 */
public class LeetCode_509 {
    /**
     * 递归
     *
     * @param n
     * @return
     */
    public static int fib(int n) {
        if (n < 2) {
            return n;
        }
        return fib(n - 1) + fib(n - 2);
    }

    /**
     * 迭代
     *
     * @param n
     * @return
     */
    public static int fib2(int n) {
        if (n < 2) {
            return n;
        }
        int lastOne = 1, lastSecond = 0;
        for (int i = 2; i <= n; i++) {
            int temp = lastSecond + lastOne;
            lastSecond = lastOne;
            lastOne = temp;
        }
        return lastOne;
    }

    public static void main(String[] args) {
        System.out.println(fib(4));
        System.out.println(fib2(4));
    }
}
[Daily Message] Leave opportunities to friends, luck to relatives, and diligence to yourself.

LeetCodet题解
技术学习

玉树临风,仙姿佚貌!

1.8k 声望
7.1k 粉丝
0 条评论
推荐阅读
牛客网高频算法题系列-BM19-寻找峰值
给定一个长度为n的数组nums,请你找到峰值并返回其索引。数组可能包含多个峰值,在这种情况下,返回任何一个所在位置即可。峰值元素是指其值严格大于左右相邻值的元素。严格大于即不能有等于假设 nums[-1] = nums...

雄狮虎豹阅读 502

封面图
Java12的新特性
Java语言特性系列Java5的新特性Java6的新特性Java7的新特性Java8的新特性Java9的新特性Java10的新特性Java11的新特性Java12的新特性Java13的新特性Java14的新特性Java15的新特性Java16的新特性Java17的新特性Java...

codecraft63阅读 11.9k

Java8的新特性
Java语言特性系列Java5的新特性Java6的新特性Java7的新特性Java8的新特性Java9的新特性Java10的新特性Java11的新特性Java12的新特性Java13的新特性Java14的新特性Java15的新特性Java16的新特性Java17的新特性Java...

codecraft32阅读 24.6k评论 1

一文搞懂秒杀系统,欢迎参与开源,提交PR,提高竞争力。早日上岸,升职加薪。
前言秒杀和高并发是面试的高频考点,也是我们做电商项目必知必会的场景。欢迎大家参与我们的开源项目,提交PR,提高竞争力。早日上岸,升职加薪。知识点详解秒杀系统架构图秒杀流程图秒杀系统设计这篇文章一万多...

王中阳Go33阅读 2.5k评论 1

封面图
Java11的新特性
Java语言特性系列Java5的新特性Java6的新特性Java7的新特性Java8的新特性Java9的新特性Java10的新特性Java11的新特性Java12的新特性Java13的新特性Java14的新特性Java15的新特性Java16的新特性Java17的新特性Java...

codecraft28阅读 15.4k评论 3

Java5的新特性
Java语言特性系列Java5的新特性Java6的新特性Java7的新特性Java8的新特性Java9的新特性Java10的新特性Java11的新特性Java12的新特性Java13的新特性Java14的新特性Java15的新特性Java16的新特性Java17的新特性Java...

codecraft13阅读 20.4k

Java9的新特性
Java语言特性系列Java5的新特性Java6的新特性Java7的新特性Java8的新特性Java9的新特性Java10的新特性Java11的新特性Java12的新特性Java13的新特性Java14的新特性Java15的新特性Java16的新特性Java17的新特性Java...

codecraft20阅读 14.5k

玉树临风,仙姿佚貌!

1.8k 声望
7.1k 粉丝
宣传栏