到第n个台阶有f(n)种方法,所有可能情况:①从n-1跳一个,②从n-2跳两个,即f(n) = f(n-1) + f(n-2)也就是斐波那契数列。采用递归+缓存,也可以说是动态规划的方案
from functools import lru_cache
class Solution:
@lru_cache
def jumpFloor(self , number: int) -> int:
# write code here
if number <= 2:
return number
return self.jumpFloor(number-1) + self.jumpFloor(number-2)