Sword_Offer 斐波拉契数列[10]
题目一:求斐波拉契数列的第n项
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# Author LQ6H
class Solution:
def Fibonacci(self,n):
if n<=0:
return 0
if n==1:
return 1
return self.Fibonacci(n-1)+self.Fibonacci(n-2)
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# Author LQ6H
class Solution:
def Fibonacci(self,n):
result=[0,1]
if n<2:
return result[n]
fibMinusOne=1
fibMinusTwo=0
fibSum=0
for i in range(2,n+1):
fibSum=fibMinusOne+fibMinusTwo
fibMinusTwo=fibMinusOne
fibMinusOne=fibSum
return fibSum
题目二:青蛙跳台阶
0x00 题目描述
题目:一次可以跳上1级台阶,也可以跳上2级台阶.求跳上一个n级台阶共有多少种调法
f(n)=f(n-1)+f(n-2)
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# Author LQ6H
class Solution:
def Fibonacci(self,n):
if n<=3:
return n
return self.Fibonacci(n-1)+self.Fibonacci(n-2)