js剑指offer-7-斐波那契数列

剑指offer7-斐波那契数列

题目描述

  大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0) 。n<=39。

const Fibonacci = n => {
    if(n < 0) return null;
    if(n === 0) return 0;
    let a = 1;
    let b = 1;
    let temp = 0;
    for(let i = 1; i < n; i++){
        temp = b;
        b = a + b;
        a = temp;
    }
    return a;
};

解析:

  实在没啥可解析的,只要注意循环的开始位即可。

猜你喜欢

转载自blog.csdn.net/julian_draxler/article/details/88075074
今日推荐