当前位置:网站首页>力扣-70.爬楼梯
力扣-70.爬楼梯
2022-04-23 11:38:00 【Node_Su】
要想到达当前楼梯,可以从n-1和n-2时的楼梯得到
首先,定义最初状态
然后,递推先前状态
最后,剩下的交给代码
超时
class Solution(object):
def climbStairs(self, n):
"""
:type n: int
:rtype: int
"""
memo = [-1] * (n+1)
if n <= 2:
return n
if memo[n] != -1:
return memo[n]
memo[n] = self.climbStairs(n - 1) + self.climbStairs(n - 2)
return memo[n]
不超时:考虑不要用函数,直接用值存
class Solution(object):
def climbStairs(self, n):
"""
:type n: int
:rtype: int
"""
if n <= 2:
return n
dp_2 = 1 # 这里表示dp-2
dp_1 = 2
for i in range(3, n + 1):
res = dp_2 + dp_1
dp_2 = dp_1
dp_1 = res
i = i + 1
return dp_1
版权声明
本文为[Node_Su]所创,转载请带上原文链接,感谢
https://blog.csdn.net/Node_Su/article/details/124361098
边栏推荐
- On lambda powertools typescript
- Nacos Foundation (9): Nacos configuration management from single architecture to microservices
- laravel-admin表单验证
- 积极参与中学机器人竞赛的意义
- docker MySQL主从备份
- 抓包整理————tcp 协议[八]
- ImportError: libX11.so.6: cannot open shared object file: No such file or directory
- 获取钉钉考勤机打卡记录
- 零钱兑换II——【LeetCode】
- rebbitMQ的简单搭建
猜你喜欢
Nacos Foundation (7): Configuration Management
nacos基础(6):nacos配置管理模型
Laravel增加自定义助手函数
nacos基础(8):登录管理
Yunna | how to manage the company's fixed assets and how to manage fixed assets
qt5.8 64 位静态库中想使用sqlite但静态库没有编译支持库的方法
nacos基础(7):配置管理
Using Baidu PaddlePaddle EasyDL to accomplish specified target recognition
简易投票系统数据库设计
Maker education for primary and middle school students to learn in happiness
随机推荐
R-drop: a more powerful dropout regularization method
Laravel增加自定义助手函数
卷积层和池化层总结
积极参与中学机器人竞赛的意义
ImportError: libX11. so. 6: cannot open shared object file: No such file or directory
oh-my-lotto
Laravel admin form validation
Interpreting the art created by robots
Cognition and R & D technology of micro robot
Nacos Basics (5): getting started with Nacos configuration
解决由于找不到amd_ags_x64.dll,无法继续执行代码。重新安装程序可能会解决此问题,地平线(Forza Horizon 5)
少儿编程结构的改变之路
kettle复制记录到结果和从结果获取记录使用
Database design of forum system
Exploring the equipment and teaching of robot education
Application of remote integrated monitoring system in power distribution room in 10kV prefabricated cabin project
Simple construction of rebbitmq
获取钉钉考勤机打卡记录
Get things technology network optimization - CDN resource request Optimization Practice
golang之筆試題&面試題01