【题解】【DP】【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?
思路:
最简单的DP问题:递归+查表
代码:
int climbStairs(int n) {
vector<int> memo(n+, -);//忘了在memo[0]没有意义的时候,数组初始大小加一。。。
return climb(n, memo);
}
int climb(int n, vector<int>& memo){//忘了加&,就会Time Limit Exceeded
if(n < ){
return -;
}
else if(n <= ){
memo[n] = n;
return n;
}
if (memo[n-] == -)
memo[n-] = climb(n-, memo);//修改了递归函数名之后没有更改其他函数体中的引用
if(memo[n-] == -)
memo[n-] = climb(n-, memo);
//return memo[n-1] + 2*memo[n-2];//等等,子问题中好像有重叠
return memo[n-] + memo[n-];
}
【题解】【DP】【Leetcode】Climbing Stairs的更多相关文章
- [LeetCode] Climbing Stairs (Sequence DP)
Climbing Stairs https://oj.leetcode.com/problems/climbing-stairs/ You are climbing a stair case. It ...
- [LeetCode] Climbing Stairs 爬梯子问题
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- Leetcode: climbing stairs
July 28, 2015 Problem statement: You are climbing a stair case. It takes n steps to reach to the top ...
- [leetcode DP]70. Climbing Stairs
一共有n个台阶,每次跳一个或者两个,有多少种走法,典型的Fibonacii问题 class Solution(object): def climbStairs(self, n): if n<0: ...
- LeetCode:Climbing Stairs(编程之美2.9-斐波那契数列)
题目链接 You are climbing a stair case. It takes n steps to reach to the top. Each time you can either c ...
- LeetCode——Climbing Stairs
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- [Leetcode] climbing stairs 爬楼梯
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- [LeetCode] Climbing Stairs 斐波那契数列
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- leetcode Climbing Stairs python
class Solution(object): def climbStairs(self, n): """ :type n: int :rtype: int " ...
- Leetcode之动态规划(DP)专题-746. 使用最小花费爬楼梯(Min Cost Climbing Stairs)
Leetcode之动态规划(DP)专题-746. 使用最小花费爬楼梯(Min Cost Climbing Stairs) 数组的每个索引做为一个阶梯,第 i个阶梯对应着一个非负数的体力花费值 cost ...
随机推荐
- BZOJ1230 [Usaco2008 Nov]lites 开关灯
区间not,求区间1的个数...线段树裸题 然而窝并不会线段树 我们可以对序列分块,每个块记录0/1的个数和tag表示又没有区间not过就好了 /*************************** ...
- [转载]WCF实现双工通信
双工(Duplex)模式的消息交换方式体现在消息交换过程中,参与的双方均可以向对方发送消息.基于双工MEP消息交换可以看成是多个基本模式下(比如请求-回复模式和单项模式)消息交换的组合.双工MEP又具 ...
- PDF 补丁丁 0.4.1.804 测试版发布:合并文件夹的图片和PDF文件,自由生成多层次书签
新的测试版增强了合并文件的功能,可以合并文件夹内的图片和PDF文件,还可以在合并文件列表上直接指定与合并文件对应的PDF书签标题.通过拖放文件项目生成多层次的PDF书签.如下图所示: 另外,新的测试版 ...
- quartz Web项目基础最简单配置
web方面的quartz 配置资料,从网上搜索出来的很难找到完整可用的代码样例.自己上传一个. IDE:Intellij tomcat jdk1.7 quartz 2.1.5 这里下载: http:/ ...
- JS中把字符串转成JSON对象的方法
在JS中,把 json 格式的字符串转成JSON对象,关键代码 json = eval('('+str+')'); <!DOCTYPE html PUBLIC "-//W3C//DTD ...
- xampp搭建服务器环境、html5新的input类型
怎么让别人看见你写的 先把你的文档放入htdocs里面 再输入网址: http://你的IP地址/文件名 就ok了例如我的 HTML5中的input类型: <input>标签规定用户可输入 ...
- uva -- 10766
一开始我感觉是模板题 是不想写的 后来发现我的模板上没有 就敲了一遍 可以忽略这句屁话 在信息学竞赛中,有关生成树的最优化问题如最小生成树等是我们经常遇到的,而对生成树的计数及其相关问题则少 ...
- POJ 1719 二分图最大匹配(记录路径)
Shooting Contest Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4097 Accepted: 1499 ...
- 4通用Makefile编写
a.c #include<stdio.h> #include "a.h" int main() { printf("hello world\n"); ...
- AOP面向切面编程
1.AOP Aop(aspect object programming)面向切面编程 功能: 让关注点代码与业务代码分离! 关注点 重复代码就叫做关注点: 切面 关注点形成的类,就叫切面(类)! 面向 ...