题解 | 斐波那契数列

斐波那契数列

https://www.nowcoder.com/practice/c6c7742f5ba7442aada113136ddea0c3?tpId=265&tqId=39214&rp=1&ru=/exam/oj/ta&qru=/exam/oj/ta&sourceUrl=%2Fexam%2Foj%2Fta%3Fpage%3D1%26tpId%3D13%26type%3D265&difficulty=undefined&judgeStatus=undefined&tags=&title=

class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param n int整型 
     * @return int整型
     */
    int Fibonacci(int n) {
        // write code here
        //https://blog.nowcoder.net/n/74fccc24fd324385a981bbd1bb53c01f
        //依据题目形式可使用递归,并且为避免递归中的重复计算,可开辟数组存储已知值
        int a=1, b=1;
        int c=0;
        if(n==1||n==2){
            return 1;
        }
        for(int count=3; count<=n; count++){
            c=a+b;
            a=b;
            b=c;
        }
        return c;
    }
};

全部评论

相关推荐

头像
08-28 09:05
门头沟学院
投递美团等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务