climb_statirs
climb_statirs.py 源码
# 爬楼梯
class Solution:
# 递归
def climbStairs(self, n: int) -> int:
if n <= 2:
return n
return self.climbStairs(n - 1) + self.climbStairs(n - 2)
# 递归+备忘录
def climbStairs2(self, n: int) -> int:
def helper(x: int) -> int:
if x <= 2:
return x
if x in mem:
return mem[x]
res = helper(x - 1) + helper(x - 2)
mem[x] = res
return res
mem = {}
return helper(n)
# 动态规划
def climbStairs3(self, n: int) -> int:
front, back = 0, 1
for _ in range(n):
front, back = back, front + back
return back
你可能感兴趣的文章
0
赞
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦