Categories
Leetcode

Fibonacci (Iterative)

function fib(n) {
if(n === 0) return 0;
if(n <= 2) return 1;

let x = 1, y = 2;
for(let i = 3; i < n; i++) {
let oldX = x;
x = y;
y = y + oldX; /*?.*/
}

return y;
}

Leave a Reply

Your email address will not be published. Required fields are marked *