> For the complete documentation index, see [llms.txt](https://garylai.gitbook.io/algorithm-and-data-structure/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://garylai.gitbook.io/algorithm-and-data-structure/problems/dynamic-programming/fibonacci-number.md).

# 509. Fibonacci Number

[509. Fibonacci Number](https://leetcode.com/problems/fibonacci-number/)

這一題是所有介紹動態規劃最基礎的一個題目，題目要求[斐波那契數](https://zh.wikipedia.org/wiki/%E6%96%90%E6%B3%A2%E9%82%A3%E5%A5%91%E6%95%B0) ，根據題目定義，可以很簡單的寫出遞迴的方法

## 遞迴

```python
class Solution:
    def fib(self, n: int) -> int:
        if n == 0:
            return 0
        elif n == 1:
            return 1
        else:
            return self.fib(n-1) + self.fib(n-2)
```

可是這個方法存在著很多的重複的子問題，所以可以用動態規劃的方法做。

## 自頂向下

```python
class Solution:
    def fib(self, n: int) -> int:
        table = defaultdict(int)
        table[1] = 1
        def traverse(n):
            # recursive
            if n <= 1:
                return table[n]
            if n in table:
                return table[n]
            else:
                table[n] = self.fib(n - 1) + self.fib(n - 2)
                return table[n]
        return traverse(n)
```

## 自底向上

```python
class Solution:
    def fib(self, n: int) -> int:
        memo = [0] * (n+1)
        memo[1] = 1
        for i in range(2, n+1):
            memo[i] = memo[i-1] + memo[i-2]

        return memo[n]
```

## 減少記憶體

```python
class Solution:    
    def fib(self, n: int) -> int:
        if n <= 1:
            return n
        first = 0
        second = 1
        for i in range(2, n + 1):
            first, second = second, first + second        
        return second
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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://garylai.gitbook.io/algorithm-and-data-structure/problems/dynamic-programming/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.
