剑指offer第十题:斐波那契数列

题目描述

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

n<=39

# -*- coding:utf-8 -*-
class Solution:
    def Fibonacci(self, n):
        # write code here
        res = [0, 1, 1, 2]
        while len(res) <= n:
            res.append(res[-1] + res[-2])

        return res[n]    
简单一句话,不要用递归。

猜你喜欢

转载自blog.csdn.net/zhangjiaxuu/article/details/80770035
今日推荐