You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/**
* @param {number} n - 整数
*/
function fibonacci(n) {
// 你的代码
}
参考答案:
function fibonacci(n) {
var a = 0;
var b = 1;
var c = a + b;
var arr = [c];
for (var i = arr.length; i < n; i++) {
arr.push(c);
a = b;
b = c;
c = a + b;
}
return arr;
}
// 再来个使用递归+ES6实现的(原创)
function fibonacci(n) {
let fn = i => i < 3 ? 1 : fn(i - 1) + fn(i - 2);
return [...Array(n).keys()].map(it => fn(it + 1));
}
function fibonacci(n) {
if (n < 0) return []
let res = []
for (let i = 0; i < n; i++) {
let num
if (i === 0) num = 0
else if (i === 1) num = 1
else num = res[i - 1] + res[i - 2]
res.push(num)
}
return res
}
Activity
liwenkang commentedon May 15, 2019
AMY-Y commentedon May 15, 2019
ahao430 commentedon May 16, 2019