Python爬楼梯,一次只能上一阶或者两阶,一共有n阶,计算一共有多少种上法,用动态规则算法,求问
3条回答 默认 最新
- Ko-walski 2022-11-07 16:25关注
n = 3 class Solution(object): def climbStairs(self, n): """ :type n: int :rtype: int """ # base case if n == 0: return(0) if n == 1: return(1) if n == 2: return(2) methods_per_steps = [1,2] for i in range(2,n+1): methods_per_steps.append(methods_per_steps[i-1]+methods_per_steps[i-2]) return(methods_per_steps[n-1]) out = Solution().climbStairs(n) print(out)
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报