[리트코드] 509. Fibonacci Number
509. Fibonacci Number
나의 풀이
/**
* @param {number} n
* @return {number}
*/
var fib = function (n) {
if (n < 2) return n;
return fib(n - 1) + fib(n - 2);
};
console.log(fib(2));
Runtime: 84 ms, faster than 49.84% of JavaScript online submissions for Fibonacci Number.
기본 방법 (효율성 bad)
/**
* @param {number} n
* @return {number}
*/
var fib = function (n) {
if (n == 0) return 0;
const fib = [0, 1, 1];
for (let i = 2; i < n; i++) {
fib[i + 1] = fib[i] + fib[i - 1];
}
return fib[fib.length - 1];
};