斐波那契数列(python)

GitHub:https://github.com/cytues/sword

源码

题目描述

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

n<=39

# -*- coding:utf-8 -*-
'''
使用递归会超时
'''
class Solution:
    def Fibonacci(self, n):
        # write code here
        # fib数列小于3的值为0,1,1
        res = [0, 1]
        while len(res) <= n:
            res.append(res[-1] + res[-2])

        return res

猜你喜欢

转载自blog.csdn.net/qq_41805514/article/details/82709075