---
# System prepended metadata

title: \#509 Fibonacci Number
tags: [leetcode, leetcode-easy]

---

# \#509 Fibonacci Number
## *費波納斯數列, f(0)=0, f(1)=1 給定n, 回傳f(n)=f(n-1)+f(n-2)*
## Log
 - build 20211006 by syhuang

## 迴圈解
 - leetcode submit runtime 81%, memory 17%
```javascript=
var fib = function(n) {
    if(n<2) return n;
    let a = 0, b = 1;
    let result = 0;
    for(let i=2; i<=n; i++){
        result = a+b;
        a = b;
        b = result;
    }
    return result;
};
```
## 遞迴解
 - leetcode submit runtime 5%, memory 56%
```javascript=
var fib = function(n) {
    if(n==0) return 0;
    if(n==1) return 1;
    return fib(n-1)+fib(n-2);
};
```
## 備註
## 參考
###### tags: `leetcode`, `leetcode-easy`