题目描述
大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0)。
n<=39
# -*- coding:utf-8 -*-
class Solution:
def Fibonacci(self, n):
# write code here
if n==0:
return 0
L=list(self.fun(n))
return L[n-1]
def fun(self,n):
num, a, b = 0, 0, 1
while num < n:
yield b
a, b = b, a + b
num = num + 1
a=Solution()
b=a.Fibonacci(0)
print(b)
使用生成器,可以减少运行时间。