斐波那契数列 牛客网 剑指Offer
题目描述大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0)。 n<=39
class Solution:
def Fibonacci(self, n):
if n <= 0:
return 0
if n == 1:
return 1
i = 2
f_1 = 0
f_2 = 1
ret = None
while i<=n:
ret = f_1 + f_2
f_1 = f_2
f_2 = ret
i += 1
return ret
转载于:https://www.cnblogs.com/vercont/p/10210371.html