leetcode在力扣 App 中打开
调试中...
调试中...
题目描述
题目描述
题解
题解
提交记录
提交记录
代码
代码
测试用例
测试用例
测试结果
测试结果
简单
相关企业
提示

请你编写一个生成器函数,并返回一个可以生成 斐波那契数列 的生成器对象。

斐波那契数列 的递推公式为 Xn = Xn-1 + Xn-2

这个数列的前几个数字是 0, 1, 1, 2, 3, 5, 8, 13 。

 

示例 1:

输入:callCount = 5
输出:[0,1,1,2,3]
解释:
const gen = fibGenerator();
gen.next().value; // 0
gen.next().value; // 1
gen.next().value; // 1
gen.next().value; // 2
gen.next().value; // 3

示例 2:

输入:callCount = 0
输出:[]
解释:gen.next() 永远不会被调用,所以什么也不会输出

 

提示:

  • 0 <= callCount <= 50
通过次数
6K
提交次数
7.4K
通过率
81.6%

相关企业

提示 1
Javascript has the concept of generators. They are critical to this problem.

提示 2
First yield 0 and 1.

提示 3
Create an infinite "while(true)" loop.

提示 4
In that loop, continuously yield the next value which is the sum of the previous two.


评论 (0)

贡献者
© 2025 领扣网络(上海)有限公司
0 人在线
行 1,列 1
运行和提交代码需要登录
callCount =
5
Source