#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param n int整型
# @return int整型
#
class Solution:
##方法一:递归,运行超时
# def Fibonacci(self , n: int) -> int:
# if n==1 or n==2:
# result=1
# if n > 2:
# result=self.Fibonacci(n-1)+self.Fibonacci(n-2) #调用函数记得加self.
# return result
# write code here
##方法二:数组
# def Fibonacci(self , n: int) -> int:
# dp=[i for i in range(n)]
# # i----------------n
# #dp[0,1,2,3]----f(1,2,3,4)
# if n==0:
# return 0
# elif n==1 or n==2:
# return 1
# else: #n=3,i=2
# dp[0]=1 #f(1)
# dp[1]=1 #f(2)
# #开始计算f(3)即dp[2]之后的
# for i in range(2,n):
# dp[i]=dp[i-1]+dp[i-2]
# return dp[-1] #最后一个
##方法三:动态规划,详见题解#斐波那契数列#的Python解法
def Fibonacci(self , n: int) -> int:
if n==0:
return 0
elif n==1:
return 1
else:
a=0
b=1
for i in range(2,n+1):
tmp=a+b
a=b
b=tmp
return b