用js刷剑指offer(斐波那契数列)
下面介绍一下什么是斐波那契数列
js代码
知道了通项公式,那代码就非常简单了
function Fibonacci(n)
{
// write code here
let pre = 1
let back = 1
let now
if (n === 0) return 0
if (n ===1 || n ===2) return 1
for (let i = 3; i <= n; i++){
now = pre + back
pre = back
back = now
}
return now
}