Skip to content

Latest commit

 

History

History
31 lines (27 loc) · 584 Bytes

Question_2648.md

File metadata and controls

31 lines (27 loc) · 584 Bytes

LeetCode Records - Question 2648 Generate Fibonacci Sequence

Attempt 1: Use yield and save the previous two values

/**
 * @return {Generator<number>}
 */
var fibGenerator = function*() {
    yield 0;
    yield 1;

    let prev1 = 0;
    let prev2 = 1;
    while (true) {
        let prev3 = prev1 + prev2;
        prev1 = prev2;
        prev2 = prev3;
        yield prev3;
    }
};

/**
 * const gen = fibGenerator();
 * gen.next().value; // 0
 * gen.next().value; // 1
 */
  • Runtime: 43 ms (Beats: 92.07%)
  • Memory: 48.87 MB (Beats: 59.71%)