剑指7:斐波那契数列

题目描述

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

n<=39

class Solution {
public:
    int Fibonacci(int n) {
        int f=0,g=1;
        while (n--){
            g+=f;
            f=g-f;
        }
        return f;
    }
};

# -*- coding:utf-8 -*-
class Solution:
    def Fibonacci(self, n):
        # write code here
        f1=0
        f2=1
        for i in range(n):
            f1,f2=f2,f1+f2
        return f1

posted on 2020-08-01 18:18  滚雪球效应  阅读(112)  评论(0编辑  收藏  举报