leetcode: solve problem 2648
This commit is contained in:
parent
48587158b2
commit
b2e06356b0
2 changed files with 28 additions and 0 deletions
19
leetcode/2648-generate-fibonacci-sequence/main.ts
Normal file
19
leetcode/2648-generate-fibonacci-sequence/main.ts
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
/**
|
||||||
|
* const gen = fibGenerator();
|
||||||
|
* gen.next().value; // 0
|
||||||
|
* gen.next().value; // 1
|
||||||
|
*/
|
||||||
|
function* fibGenerator(): Generator<number, any, number> {
|
||||||
|
let a = 0;
|
||||||
|
let b = 1;
|
||||||
|
let iter = 0;
|
||||||
|
while (true) {
|
||||||
|
if (iter < 2) {
|
||||||
|
yield iter;
|
||||||
|
iter++;
|
||||||
|
} else {
|
||||||
|
yield a + b;
|
||||||
|
[a, b] = [b, a + b];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
9
leetcode/2648-generate-fibonacci-sequence/readme.md
Normal file
9
leetcode/2648-generate-fibonacci-sequence/readme.md
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
# 2648. Generate Fibonacci Sequence
|
||||||
|
|
||||||
|
[Leetcode](https://leetcode.com/problems/generate-fibonacci-sequence/description/)
|
||||||
|
|
||||||
|
Write a generator function that returns a generator object which yields the fibonacci sequence.
|
||||||
|
|
||||||
|
The fibonacci sequence is defined by the relation Xn = Xn-1 + Xn-2.
|
||||||
|
|
||||||
|
The first few numbers of the series are 0, 1, 1, 2, 3, 5, 8, 13.
|
Loading…
Reference in a new issue