题解 | #斐波那契数列#

斐波那契数列

https://www.nowcoder.com/practice/c6c7742f5ba7442aada113136ddea0c3

class Solution {
public:
    int Fibonacci(int n) {
        if (n == 1 || n == 2) {
            return 1;
        }
        int n1 = 1, n2 = 1, n3;
        for (int i = 3; i <= n; ++i) {
            n3 = n1 + n2;
            n1 = n2;
            n2 = n3;
        }
        return n3;
    }
};

递推就行了

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务