LeetCode 70 Climbing Stairs(爬楼梯)(动态规划)(*)
翻译
你正在爬一个楼梯。
它须要n步才干究竟顶部。
每次你能够爬1步或者2两步。
那么你有多少种不同的方法爬到顶部呢?
原文
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?
分析
动态规划基础题,首先设置3个变量用于转换:
int dp1 = 1, dp2 = 2, dpWay = 0;
依据题意,一次仅仅能是一步或两步。所以当n等于2时,有两种走法:1+1,2。
if (n <= 1) return dp1;
if (n == 2) return dp2;
从3開始,由于能够直接获得它的步数结果。所以直接写成:
while ((n--)-2) {
}
终于里面的变化方式为:
dpWay = dp1 + dp2;
dp1 = dp2;
dp2 = dpWay;
上一篇博客:LeetCode 206 Reverse Linked List(反转链表)(四步将递归改写成迭代)(*) ,介绍了怎样将递归改写成迭代,看过的童鞋应该会认为很easy的。那么这里再来转换一次:
int climbStairsIter(int n, int dpWay,int dp1, int dp2) {
if (n <= 1) return dp1;
if (n == 2) return dp2;
if ((n--) - 2) {
dpWay = dp1 + dp2;
dp1 = dp2;
dp2 = dpWay;
return climbStairsIter(n, dpWay, dp1, dp2);
}
else return dpWay;
}
int climbStairs(int n) {
return climbStairsIter(n, 0,1,2);
}
由于这里的參数涉及到运行前面顺序,所以不妨单独列出来了,只是这样看来略不简洁呐。
代码
class Solution {
public:
int climbStairs(int n) {
int dp1 = 1, dp2 = 2, dpWay = 0;
if (n <= 1) return dp1;
if (n == 2) return dp2;
while ((n--) - 2) {
dpWay = dp1 + dp2;
dp1 = dp2;
dp2 = dpWay;
}
return dpWay;
}
};
LeetCode 70 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 70. Climbing Stairs 爬楼梯 (递归,记忆化,动态规划)
题目描述 要爬N阶楼梯,每次你可以走一阶或者两阶,问到N阶有多少种走法 测试样例 Input: 2 Output: 2 Explanation: 到第二阶有2种走法 1. 1 步 + 1 步 2. 2 ...
- 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 ...
- Climbing Stairs爬楼梯——动态规划
题目描写叙述: 初阶:有n层的台阶,一開始你站在第0层,每次能够爬两层或者一层. 请问爬到第n层有多少种不同的方法? 进阶:假设每次能够爬两层.和倒退一层,同一个位置不能反复走,请问爬到第n层有多少种 ...
- 70. Climbing Stairs爬楼梯
网址:https://leetcode.com/problems/climbing-stairs/ 其实就是斐波那契数列,没什么好说的. 注意使用3个变量,而不是数组,可以节约空间. class So ...
- Leetcode#70. Climbing Stairs(爬楼梯)
题目描述 假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数. 示例 1: 输入: 2 输出: 2 解 ...
- LN : leetcode 70 Climbing Stairs
lc 70 Climbing Stairs 70 Climbing Stairs You are climbing a stair case. It takes n steps to reach to ...
- 42. leetcode 70. Climbing Stairs
70. Climbing Stairs You are climbing a stair case. It takes n steps to reach to the top. Each time y ...
随机推荐
- SQL CREATE INDEX
n relational database, Index will be the important mechanism for boosting performance. Index is impo ...
- TZOJ 1072: 编辑距离(动态规划)
1072: 编辑距离 时间限制(普通/Java):1000MS/10000MS 内存限制:65536KByte 总提交: 917 測试通过:275 描写叙述 如果字符串的 ...
- Git使用帮助
1. 本地创建新项目 git init git add . git commit -m "First commit" 2. 本地代码同步Push到远程仓库 git remote a ...
- java中循环控制结构
1. break结束break所在循环 for(i……) { for(j……) { break; //结束循环j } } 2.带标签的break. java中的标签只用在循环语句前面. outer: ...
- SpringMVC请求参数和响应结果全局加密和解密
前提 前段时间在做一个对外的网关项目,涉及到加密和解密模块,这里详细分析解决方案和适用的场景.为了模拟真实的交互场景,先定制一下整个交互流程.第三方传输(包括请求和响应)数据报文包括三个部分: 1.t ...
- 缺少网络连接需要的Windows套接字注册表项(浏览器无法连网)
缺少网络连接需要的Windows套接字注册表项(浏览器无法连网) CreateTime--2018年4月25日14:17:42 Author:Marydon 1.异常信息 此计算机上缺少一个或多个 ...
- HDU 2647--Reward【拓扑排序】
Reward Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- webservice调用的四种方式
因为数据在网络上传输都是通过xml形式的,本质都是把数据封装然后通过xml传输,接收到的也是xml文件,1 和 4 让程序员屏蔽了处理xml文件,而2 和3需要程序员自己写请求体 ,还要处理返回的xm ...
- Android 读取<meta-data>元素中的数据
众所周知, 每个组件都有<meta-data>元素, 用于接收一些外部数据(eg: appKey), 那其中的值应该怎么读取呢. 1> Application <applica ...
- window.onunload | window.onbeforeunload
先引述一段jQuery 官方对于onunload的评述: The unload event is sent to the window element when the user navigates ...