class Solution(object):
def fib(self, n):"""
:type n:int:rtype:int"""
# 1.递归
if n ==0:return0if n ==1:return1return self.fib(n-1)+ self.fib(n-2)
class Solution(object):
def fib(self, n):"""
:type n:int:rtype:int"""
# 2.记忆化递归
memory =[0for i in range(n+1)]return self.fibMemory(n, memory)
def fibMemory(self, n, memory):if memory[n]>0:return memory[n]if n ==0:
memory[0]=0
elif n ==1:
memory[1]=1else:
memory[n]= self.fibMemory(n-1, memory)+ self.fibMemory(n-2, memory)return memory[n]
class Solution(object):
def fib(self, n):"""
:type n:int:rtype:int"""
# 3.动态规划
if n==0:return0if n==1:return1
dp =[0for i in range(n+1)]
dp[1]=1for i in range(2, n+1):
dp[i]= dp[i-1]+ dp[i-2]return dp[n]
class Solution(object):
def fib(self, n):"""
:type n:int:rtype:int"""
# 4.动态规划+滚动数组
if n<2:return n
p, q, r =0,0,1for i in range(2, n+1):
p, q = q, r
r = p + q
return r