当前位置:网站首页>LeetCode_70 爬楼梯
LeetCode_70 爬楼梯
2022-04-21 20:07:00 【W__winter】
1、题目:爬楼梯
假设你正在爬楼梯。需要 n 阶你才能到达楼顶。
每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢?

2、解题思路
爬到第一层楼梯有一种方法,爬到二层楼梯有两种方法。
那么第一层楼梯再跨两步就到第三层 ,第二层楼梯再跨一步就到第三层。
所以到第三层楼梯的状态可以由第二层楼梯 和 到第一层楼梯状态推导出来,那么就可以想到动态规划了。
动规五部曲
1、确定dp数组以及下标的含义, 爬到第i层楼梯,有dp[i]种方法
2、确定递推公式,
首先是dp[i - 1],上i-1层楼梯,有dp[i - 1]种方法,那么再一步跳一个台阶不就是dp[i]了么。
还有就是dp[i - 2],上i-2层楼梯,有dp[i - 2]种方法,那么再一步跳两个台阶不就是dp[i]了么。
那么dp[i]就是 dp[i - 1]与dp[i - 2]之和!
所以dp[i] = dp[i - 1] + dp[i - 2] 。
3、dp数组如何初始化,dp[1] = 1,dp[2] = 2
4、确定遍历顺序,从递推公式dp[i] = dp[i - 1] + dp[i - 2];中可以看出,遍历顺序一定是从前向后遍历的
5、举例推导dp数组,举例当n为5的时候,dp table(dp数组)应该是这样的

3、代码
class Solution {
public:
int climbStairs(int n) {
if (n <= 1) return n; // 因为下面直接对dp[2]操作了,防止空指针
vector<int> dp(n + 1);
dp[1] = 1;
dp[2] = 2;
for (int i = 3; i <= n; i++) { // 注意i是从3开始的
dp[i] = dp[i - 1] + dp[i - 2];
}
return dp[n];
}
};
版权声明
本文为[W__winter]所创,转载请带上原文链接,感谢
https://blog.csdn.net/W__winter/article/details/124329793
边栏推荐
- Solution of "unable to load and save data" in Eldon law ring
- Jerry's VDDIO_ SYSVDD_ DC14 system voltage configuration description [chapter]
- vmware-vmx.exe无法结束进程
- VIM from dislike to dependence (6) -- insertion mode
- IaaS,PaaS,SaaS 的区别
- 浅谈多核CPU、多线程与并行计算
- R language data analysis from entry to advanced: (8) data format conversion of data cleaning skills (including the conversion between wide data and long data)
- 英音与美音的区别【转】
- Jerry's switch interrupt function using unshielded interrupt [chapter]
- C# Mandelbrot和Julia分形图像生成程序更新到2010-9-14版 支持多线程计算 多核处理器
猜你喜欢

Nodejs notes 1

CUDA02 - 访存优化和Unified Memory

Your independent station conversion rate is low? Three tips to help improve conversion

80. (leaflet chapter) leaflet calls the PostGIS data layer published by GeoServer

Wild road play QT, episode 31, glass cleaning game

MySQL error 2005

Fiddler's solution to the failure of capturing PC wechat applet

Surface point cloud normal

Know that Chuangyu issued a heavy strategic plan to build a practical defense system for continuous exchange of fire

JUC queue interface and its implementation class
随机推荐
How to check the slow response of the system with high CPU?
The difference between English and American pronunciation [turn]
测试while(u--);和while(u)u--;的区别
Considerations for index creation
TCP example of grpc implemented by golang
Meaning of stripe in image
GBase 8a设置 group_concat_max_len 参数后报错解决方案
Cuda02 - memory access optimization and unified memory
Interface non idempotent solution
微信服务端配置
STL container (I) -- vector
Redis can send verification codes and limit the number of times sent every day
R language data analysis from entry to advanced: (8) data format conversion of data cleaning skills (including the conversion between wide data and long data)
PyCharm failed to create JVM
Jerry's VDDIO_ SYSVDD_ DC14 system voltage configuration description [chapter]
如何让网卡后门搞死一个系统,让你知道网卡是个多么厉害的角色
Fiddler's solution to the failure of capturing PC wechat applet
接口非幂等性解决
第九届”大唐杯“全国大学生移动通信5G技术大赛省赛获奖名单公示
How to judge whether the nbit bit of int type value is 1 or 0