【题解】【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 ...
随机推荐
- Snappy压缩
Snappy压缩时,碰到不能解压问题,所用服务器Tomcat8.经验证,降低Tomcat版本为7,才可正常解压文件. 若碰到偶尔不能解压的问题,试着换个浏览器试试.
- Android——关于Activity跳转的返回(无返回值和有返回值)——有返回值
说明: 跳转页面,并将第一页的Edittext输入的数据通过按钮Button传到第二页用Edittext显示,点击第二页的 返回按钮Button返回第一页(改变第二页的Edittext的内容会传至第一 ...
- HDU 2795 单点更新,区间优先查找(想法)
Billboard Time Limit: 20000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- Ajax入门
实例如下: <html> <head> <script type="text/javascript"> function loadXMLDoc( ...
- ubuntu 14.04 难用的vi
在插入状态下,按方向键出来的结果竟然是大写的字母ABCD,这是因为在ubuntu中其实没装vi,只装了vim-tiny,在系统上,vi仅仅是vim的一个别名. 这时候需要自己安装完整版的 vim su ...
- WEKA使用教程(界面工具的用法)
WEKA使用教程 目录 1. 简介2. 数据格式3.数据准备4. 关联规则(购物篮分析)5. 分类与回归6. 聚类分析 1. 简介 WEKA的全名是怀卡托智能分析环境(Waikato Environm ...
- Java并发编程(三)后台线程(Daemon Thread)
后台线程,守护线程(Daemon Thread) 所谓的后台线程,就是指这种线程并不属于程序中不可或缺的部分,因此当所有的非后台线程结束时,程序也就终止了,同时会杀死进程中的所有后台线程.通过setD ...
- 【C语言学习】-05 二维数组、字符串数组、多维数组
⼆二维数组.字符串数组.多维数组
- lucas 定理学习
大致意思就是求组合数C(n , m) % p的值, p为一个偶数 可以将组合数的n 和 m都理解为 p 进制的表示 n = ak*p^k + a(k-1)*p^(k-1) + ... + a1*p ...
- jQuery 1.7_20120209 学习笔记
html([val|fn]) parameters: function(index,html) 此函数返回一个html字符串,接受两个参数,index为元素在集合中的索引位置,html为原先的html ...