Skip to content

Commit 2cad78a

Browse files
committed
LC 2648. Generate Fibonacci Sequence (JS)
1 parent 4ddc76e commit 2cad78a

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,7 @@ Solutions to LeetCode problems. The first column links to the problem in LeetCod
527527
| [2635. Apply Transform Over Each Element in Array][lc2635] | 🟢 Easy | [![js](res/js.png)][lc2635js] |
528528
| [2636. Promise Pool][lc2636] | 🟠 Medium | [![js](res/js.png)][lc2636js] |
529529
| [2637. Promise Time Limit][lc2637] | 🟢 Easy | [![js](res/js.png)][lc2637js] |
530+
| [2648. Generate Fibonacci Sequence][lc2648] | 🟢 Easy | [![js](res/js.png)][lc2648js] |
530531
| [2665. Counter II][lc2665] | 🟢 Easy | [![js](res/js.png)][lc2665js] |
531532
| [2666. Allow One Function Call][lc2666] | 🟢 Easy | [![js](res/js.png)][lc2666js] |
532533
| [2667. Create Hello World Function][lc2667] | 🟢 Easy | [![js](res/js.png)][lc2667js] |
@@ -1680,6 +1681,8 @@ Solutions to LeetCode problems. The first column links to the problem in LeetCod
16801681
[lc2636js]: leetcode/promise-pool.js
16811682
[lc2637]: https://leetcode.com/problems/promise-time-limit/
16821683
[lc2637js]: leetcode/promise-time-limit.js
1684+
[lc2648]: https://leetcode.com/problems/generate-fibonacci-sequence/
1685+
[lc2648js]: leetcode/generate-fibonacci-sequence.js
16831686
[lc2665]: https://leetcode.com/problems/counter-ii/
16841687
[lc2665js]: leetcode/counter-ii.js
16851688
[lc2666]: https://leetcode.com/problems/allow-one-function-call/
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// 2648. Generate Fibonacci Sequence
2+
// 🟢 Easy
3+
//
4+
// https://leetcode.com/problems/generate-fibonacci-sequence/
5+
//
6+
// Tags: Javascript
7+
8+
// A generator that builds its next value as the sum of the previous two.
9+
//
10+
// Time complexity: O(n) - Each call takes O(1), n calls will take O(n)
11+
// Space complexity: O(1) - The generator only stores two number values.
12+
//
13+
// Runtime 58 ms Beats 56.65%
14+
// Memory 41.3 MB Beats 97.60%
15+
/**
16+
* @return {Generator<number>}
17+
*/
18+
var fibGenerator = function* () {
19+
let cur = 0;
20+
let next = 1;
21+
while (true) {
22+
next = cur + next;
23+
cur = next - cur;
24+
yield next - cur;
25+
}
26+
};
27+
28+
/**
29+
* const gen = fibGenerator();
30+
* gen.next().value; // 0
31+
* gen.next().value; // 1
32+
*/

0 commit comments

Comments
 (0)