【题解】【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 ...
随机推荐
- BZOJ3933 [CQOI2015]多项式
$\sum_{k = 0} ^ {n} a_kx^k = \sum_{k = 0} ^ {n} b_k(x - t)^k \Leftrightarrow \sum_{k = 0} ^ {n} a_k( ...
- 1800: [Ahoi2009]fly 飞行棋
#include<cstdio> #include<iostream> using namespace std; ],n,ans; int main() { scanf(&qu ...
- TopCoder SRM 583 TurnOnLamps
读错题了有没有呀,原来 lamps 是在边上的呀,当成是在点上的了,无语. 直接一个dfs 就可以 从叶子节点开始,如果有必要转换 lamp 的状态则加一个仅包含 这个 lamp 的段 然后向上扩展, ...
- js unix时间戳转换
一.unix时间戳转普通时间: var unixtime=1358932051; var unixTimestamp = new Date(unixtime* 1000); commonTime = ...
- 从报错“无效操作,连接被关闭”探究Transaction的Timeout超时机制
1.报错如下:Invalid Operation the connection is closed,无效操作,连接被关闭.这个错误是并不是每次都报,只有在复杂操作.大事务的情况下才偶然报出来. sta ...
- IT公司100题-6-根据上排给出十个数,在其下排填出对应的十个数
问题描述: 给你10分钟时间,根据上排给出十个数,在其下排填出对应的十个数要求下排每个数都是先前上排那十个数在下排出现的次数.上排的十个数如下:[0,1,2,3,4,5,6,7,8,9] 举一个例子, ...
- string.format
string.Format("{0:#,0}", c.num), //千分号,有小数就保留2位小数 string.Format("{0:N2}", c.amou ...
- Rhel6-haproxy+keepalived配置文档
系统环境: rhel6 x86_64 iptables and selinux disabled 主机: 192.168.122.119:haproxy,keepalived server19.exa ...
- 凭借K2 SmartObject框架,在SharePoint中集成数据
随着SharePoint 2013的发布,Microsoft已提供Business Connectivity Services(BCS)增强功能以及外部列表功能,确保您可以更简单地在SharePoin ...
- 第二周 PSP项目计划
项目计划总结: 日期|任务 听课 编写程序 阅读相关书籍 网上查找资料 日总计 周一 2 1 2 5 周二 1 1 周三 2 2 周四 2 2 4 周五 ...