n个台阶,每次可以走一步或者两步,总共有多少种走法。

第一感觉想到的是递归,n为1的时候1种,2的时候2中。其他时候就是 fun(n) = fun(n-1) + fun(n-2);递归的代码很简单。如下

class Solution {
public:
int climbStairs(int n) {
if (n == ) return ;
if (n == ) return ;
if (n == ) return ;
else
return climbStairs(n-) + climbStairs(n-);
}
};

但是超时了。看了tag提示DP。于是就分分钟改为DP

class Solution {
public:
int climbStairs(int n) {
if (n == ) return ;
if (n == ) return ;
if (n == ) return ;
vector<int> ans(n);
ans[] = ; ans[] = ;
for (int i = ; i < n; ++i)
{
ans[i] = ans[i-] + ans[i-];
}
return ans[n-];
}
};

果断AC。

leetcode[68] Climbing Stairs的更多相关文章

  1. [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 ...

  2. [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 ...

  3. leetCode 70.Climbing Stairs (爬楼梯) 解题思路和方法

    Climbing Stairs  You are climbing a stair case. It takes n steps to reach to the top. Each time you ...

  4. 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 ...

  5. Leetcode#70. Climbing Stairs(爬楼梯)

    题目描述 假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数. 示例 1: 输入: 2 输出: 2 解 ...

  6. 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 ...

  7. [LeetCode OJ]-Climbing Stairs

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...

  8. [leetcode] 14. Climbing Stairs

    这道题leetcode上面写着是DP问题,问题是我一开始写了个简单的递归结果直接超时,所以没办法只好拿迭代来做了.题目如下: You are climbing a stair case. It tak ...

  9. LeetCode 70 Climbing Stairs(爬楼梯)(动态规划)(*)

    翻译 你正在爬一个楼梯. 它须要n步才干究竟顶部. 每次你能够爬1步或者2两步. 那么你有多少种不同的方法爬到顶部呢? 原文 You are climbing a stair case. It tak ...

随机推荐

  1. java设计模式:观察者模式

    package Observer; public class Test { /** * client测试类别 * 观察者模式一般由四部分组成: * 1摘要观察员(教科书被称为一般"Subje ...

  2. 左右v$datafile和v$tempfile中间file#

    v$datafile关于存储在文件中的数据视图的信息,v$tempfile查看存储在一个临时文件中的信息. 有两种观点file#现场,首先来看看官方文件的定义: V$DATAFILE This vie ...

  3. NSIS:检查某注册表键是否存在

    原文NSIS:检查某注册表键是否存在 ;定义注册表主键!define HKEY_CLASSES_ROOT           0x80000000!define HKEY_CURRENT_USER   ...

  4. Ajax实践之用户是否存在

    关于Ajax在之前的学习中,已经对它的基础知识有了初步的了解.仅仅是欠实践. 那么接下来就让实践来检验一下真理吧! 基础见:http://blog.csdn.net/liu_yujie2011com/ ...

  5. Java设计模式偷跑系列(十八)建模和责任链模式的实现

    转载请注明出处:http://blog.csdn.net/lhy_ycu/article/details/40018231 责任链模式(ChainOfResponsibility): 有多个对象,每一 ...

  6. 设计模式Template Method模式(Template Method)摘录

    23种子GOF设计模式一般分为三类:创建模式.结构模型.行为模式. 创建模式抽象的实例.怎样创建.组合和表示它的那些对象.一个类创建型模式使用继承改变被实例化的类,而一个对象创建型模式将实例化托付给还 ...

  7. Parse 和 Swift 搭建一个像 Instagram

    如何用 Parse 和 Swift 搭建一个像 Instagram 那样的应用?   [编者按]本篇文章作者是Reinder de Vries,既是一名企业家,也是优秀的程序员,发表多篇应用程序的博客 ...

  8. STUN协议简介

    STUN简要 STUN(Simple Traversal of UDP over NATs,NAT 的UDP简单穿越)是一种网络协议.它同意位于NAT(或多重NAT)后的client找出自己的公网地址 ...

  9. Qt Quick 组件和动态创建的对象具体的解释

    在<Qt Quick 事件处理之信号与槽>一文中介绍自己定义信号时,举了一个简单的样例.定义了一个颜色选择组件,当用户在组建内点击鼠标时,该组件会发出一个携带颜色值的信号,当时我使用 Co ...

  10. 面向对象三大特征之继承(extends)——Java笔记(六)

    继承:            从一般到特殊的关系,是一种拓展关系,子类对象是父类的一种,也可称为”is a“的关系 泛化:         把子类里的共性抽取到父类里的来的过程 特化:         ...