剑指offer系列——斐波那契数列

class Solution {
public:
    int Fibonacci(int n) {
        int ans = 0;
        int a = 0,b = 1;
        if(n == 0){
            return 0;
        }else if(n == 1){
            return 1;
        }else{
            for(int i = 2;i <= n;i++){
                ans = a + b;
                a = b;
                b = ans;
            }
        return ans;
        }
    }
};
发布了51 篇原创文章 · 获赞 29 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/qwer7512090/article/details/104910136
今日推荐