动态规划_leetcode70
Posted AceKo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了动态规划_leetcode70相关的知识,希望对你有一定的参考价值。
class Solution(object):
def climbStairs(self, n):
"""
:type n: int
:rtype: int
"""
self.memo = [-1 for i in range(n+1)]
return self.calcWays1(n)
def calcWays1(self,n):
if n == 1:
return 1
if n == 2:
return 2
else:
return self.climbStairs(n-1) + self.climbStairs(n-2)
def calcWays2(self, n):
if n == 0 or n == 1:
return 1
if self.memo[n] == -1:
self.memo[n] = self.calcWays2(n-1) + self.calcWays2(n-2)
def calcWays3(self, n):
memo = [-1 for i in range(n+1)]
memo[1] = 1
memo[2] = 2
for i in range(3,n+1):
memo[i] = memo[i-1] + memo[i-2]
return memo[n]
以上是关于动态规划_leetcode70的主要内容,如果未能解决你的问题,请参考以下文章
动态规划系列 Leetcode 70. Climbing Stairs
LeetCode 70 Climbing Stairs(爬楼梯)(动态规划)(*)