2775. Generate Fibonacci Sequence
My accepted JavaScript solution to LeetCode problem 2775, Generate Fibonacci Sequence, running in 41ms.
- Difficulty: Easy
- JavaScript
- Runtime 41ms
- Memory 53.4MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
JavaScript
Accepted on LeetCode — runtime 41ms, memory 53.4MB, accepted 2025-12-31.
/**
* @return {Generator<number>}
*/
var fibGenerator = function*() {
let a = 0, b = 1;
while (true) {
yield a;
[a, b] = [b, a + b];
}
};
/**
* const gen = fibGenerator();
* gen.next().value; // 0
* gen.next().value; // 1
*/