# 509.Fibonacci-Number

## 509. Fibonacci Number

## 题目地址

<https://leetcode.com/problems/fibonacci-number/>

## 题目描述

```
The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is,

F(0) = 0,   F(1) = 1
F(N) = F(N - 1) + F(N - 2), for N > 1.
Given N, calculate F(N).

Example 1:
Input: 2
Output: 1
Explanation: F(2) = F(1) + F(0) = 1 + 0 = 1.

Example 2:
Input: 3
Output: 2
Explanation: F(3) = F(2) + F(1) = 1 + 1 = 2.

Example 3:
Input: 4
Output: 3
Explanation: F(4) = F(3) + F(2) = 2 + 1 = 3.

Note:
0 ≤ N ≤ 30.
```

## 代码

### Approach #1 Recursion

**Complexity Analysis**

* Time complexity : O(2^N)
* Space complexity : O(N)

```java
class Solution {
  public int fib(int N) {
        if (N <= 1) {
      return N;
    }
    return fib(N - 1) + fib(N - 2);
  }
}
```

### Approach #2 Bottom-Up Approach using memoization

```java
class Solution {
  public int fib(int N) {
    if (N <= 1) {
      return N;
    }
    return memoize(N);
  }

  public int memoize(int N) {
    int[] cache = new int[N + 1];
    cache[1] = 1;

    for (int i = 2; i <= N; i++) {
      cache[i] = cache[i - 1] + cache[i - 2];
    }
    return cache[N];
  }
}
```

### Approach #3 Top-Down Approach using Memoization

```java
class Solution {
  private Integer[] cache = new Integer[31];

  public int fib(int N) {
    if (N <= 1) {
      return N;
    }
    cache[0] = 0;
    cache[1] = 1;
    return memoize(N);
  }

  public int memoize(int N) {
    if (cache[N] != null) {
      return cache[N];
    }
    cache[N] = memoize(N-1) + memoize(N-2);
    return memoize(N);
  }
}
```

### Approach #4 Iterative Top-Down Approach

```java
class Solution {
  if (N <= 1) {
    return N;
  }
  if (N == 2) {
    return 1;
  }

  int current = 0;
  int prev1 = 1;
  int prev2 = 1;

  for (int i = 3; i <= N; i++) {
    current = prev1 + prev2;
    prev2 = prev1;
    prev1 = current;
  }

  return current;
}
```

### Approach #5 Matrix Exponetiation

```java
class Solution {
    int fib(int N) {
        if (N <= 1) {
          return N;
        }
        int[][] A = new int[][]{{1, 1}, {1, 0}};
        matrixPower(A, N-1);

        return A[0][0];
    }

    void matrixPower(int[][] A, int N) {
        if (N <= 1) {
          return;
        }
        matrixPower(A, N/2);
        multiply(A, A);

        int[][] B = new int[][]{{1, 1}, {1, 0}};
        if (N%2 != 0) {
            multiply(A, B);
        }
    }

    void multiply(int[][] A, int[][] B) {
        int x = A[0][0] * B[0][0] + A[0][1] * B[1][0];
        int y = A[0][0] * B[0][1] + A[0][1] * B[1][1];
        int z = A[1][0] * B[0][0] + A[1][1] * B[1][0];
        int w = A[1][0] * B[0][1] + A[1][1] * B[1][1];

        A[0][0] = x;
        A[0][1] = y;
        A[1][0] = z;
        A[1][1] = w;
    }
}
```

### Approach #6 Math

```java
class Solution {
    public int fib(int N) {
        double goldenRatio = (1 + Math.sqrt(5)) / 2;
        return (int)Math.round(Math.pow(goldenRatio, N)/ Math.sqrt(5));
    }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://wentao-shao.gitbook.io/leetcode/array/509.fibonacci-number.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
