[Leetcode] climbing stairs 爬楼梯
You are climbing a stair case. It takes n steps to reach to the top.
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
题意:有n阶楼梯,每次可以走一步或者两步,总共有多少种方法。
思路:动态规划。维护一个一维数组dp[n+1],dp[0]为n=0时的情况,dp[ i ]为到达第i阶台阶总共的方法。例,当n=4时,如下图,很快就可以推出状态转移方程为:dp[i]=dp[i-1]+dp[i-2] (i >=2)。
代码如下:
class Solution {
public:
int climbStairs(int n)
{
vector<int> dp(n+,);
dp[]=,dp[]=;
for(int i=;i<n+;i++)
{
dp[i]=dp[i-]+dp[i-];
}
return dp[n];
}
};
[Leetcode] climbing stairs 爬楼梯的更多相关文章
- [LeetCode] 70. Climbing Stairs 爬楼梯问题
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- [LeetCode] 70. Climbing Stairs 爬楼梯
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- [LeetCode] Min Cost Climbing Stairs 爬楼梯的最小损失
On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once you pay ...
- [LeetCode] 746. Min Cost Climbing Stairs 爬楼梯的最小损失
On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once you pay ...
- [LeetCode] Climbing Stairs 爬梯子问题
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- climbing stairs(爬楼梯)(动态规划)
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- Climbing Stairs爬楼梯——动态规划
题目描写叙述: 初阶:有n层的台阶,一開始你站在第0层,每次能够爬两层或者一层. 请问爬到第n层有多少种不同的方法? 进阶:假设每次能够爬两层.和倒退一层,同一个位置不能反复走,请问爬到第n层有多少种 ...
- LeetCode 70. Climbing Stairs爬楼梯 (C++)
题目: You are climbing a stair case. It takes n steps to reach to the top. Each time you can either cl ...
- [leetcode]70. Climbing Stairs爬楼梯
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
随机推荐
- vi/vim连续注释
知识点: 1-可视块模式方法 2-替换方法 3-自定义快捷键方式 今天刚好重新在linux上手工搭建完Lamp环境,用来下vi操作,一段时间不用就有些生疏了,正好经常要注释,回顾下自己会的方法,小结一 ...
- labview初始学习过程中遇到串口读取框红蓝色交替闪烁的处理
labview工程的程序框图VISA串口读取框红蓝交替闪烁,前面板接收数据错乱,或者是接受不了,这是你不小心设置了断点.
- Unicode控制字符
Unicode控制字符就是特殊的Unicode字符 控制字符转义代码对照表 Unicode-控制字符 LRM RLM ZWJ ZWNJ LRE LRO RLO PDF NADS ...
- R语言绘图:在地图上绘制散点图
使用ggplot2在地图上绘制散点图 ######*****绘制散点图代码*****####### options(baidumap.key = '**************') #设置密钥 bei ...
- 2018年第九届蓝桥杯【C++省赛B组】【第二题:明码】
参考:https://blog.csdn.net/qq_34202873/article/details/79784242 #include <bits/stdc++.h> using n ...
- Hbase表格设计
Rowkey设计 Region: 基于RowKey的分区,可理解成MySQL的水平切分. 每个Region Server就是Hadoop集群中一台机器上的一个进程. 比如我们的有1-300号的RowK ...
- 两个完整的jquery slide多方面滑动效果实例
实例1,需要引用jquery-ui.js <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" &qu ...
- CSS里一个奇怪的属性
事情是这样的,在一个手机界面的制作中,我发现按钮点击后总会出现一个边框,于是开始搜索解决方案.搜到的解决方案是这样的. a:focus,input:focus{ -webkit-tap-highlig ...
- APPium-python实例(记录)
https://github.com/appium/sample-code/tree/master/sample-code/examples/python
- Android基本组件
①Activity和View负责与用户交互 ②Service通常位于后台,拥有独立的生命周期,为其他组件提供后台服务和监控其他组件运行状态 ③BroadcastReceiver广播消息接收器,类似事件 ...