Last active
June 22, 2020 09:16
-
-
Save guxuerui/ab79d1c08884f2293d9918d889ec4927 to your computer and use it in GitHub Desktop.
菲波那切数列
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// const fib = (num) => { | |
// if (num <= 0) return 0; | |
// if (num === 1 || num === 2) return 1; | |
// if (num > 2) { | |
// let prev = 1, next = 1; | |
// for (let i = 3; i <= num; i++) { | |
// let sum = prev + next; | |
// prev = next; | |
// next = sum; | |
// } | |
// return next; | |
// } | |
// }; | |
let map = { | |
0: 0, | |
1: 1 | |
}; | |
const fib = (num) => { | |
if (map[num] === undefined) map[num] = fib(num - 1) + fib(num - 2); | |
return map[num]; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment