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中System类简介(转)

    上次面试中遇到的一个问题,问到System.out.println()中的out是不是内部类[不是内部类],当时就给问蒙了,直观感觉out应该是System类的一个属性,跟内部类有什么关系?而且之前整 ...

  2. 【IOS 开发】Object-C 入门 Xcode 环境具体解释

    作者 : 韩曙亮 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/38424965 一. Xcode 环境安装 与 project ...

  3. 王立平-NGUI

    NGUI: UI插入 UI : 人机交互界面 UI大部分是2D 眼下就我们总结这些.也许会增加 版权声明:本文博主原创文章,博客,未经同意不得转载.

  4. Installshield自动安装IIS组件

    原文:Installshield自动安装IIS组件 一. 工程类型:IS2010 installscript 二.原理: 1. XP和 Server2003系统:由于系统默认没有自带IIS组件,一般情 ...

  5. poj 3400 Dropping the stones

    //next_permutation全阵列 # include <stdio.h> # include <algorithm> # include <string.h&g ...

  6. What is WCF

    几个博客前,都是关于WCF零散的知识.要了解下下面的宏,什么是WCF? WCF:Windows Communication Foundation(WCF)框架. 1.WCF体系框架 2.框架分析 2. ...

  7. .NET中使用Redis(二)

    很久以前写了一篇文章 .NET中使用Redis 介绍了如何安装Redis服务端,以及如何在.NET中调用Redis读取数据.本文简单介绍如何设计NoSQL数据库,以及如何使用Redis来存储对象. 和 ...

  8. hdu 3068 最长回文(manachar模板)

    Problem Description 给出一个只由小写英文字符a,b,c...y,z组成的字符串S,求S中最长回文串的长度.回文就是正反读都是一样的字符串,如aba, abba等   Input 输 ...

  9. Struts2流程

    Struts2流程 1.client浏览器初始化时发出HTTP请求 2.依据web.xml配置,上述请求被FilterDispatcher接收 3.依据struts.xml配置,找到须要调用的Acti ...

  10. 使用ArcGIS API for Silverlight + Visifire绘制地图统计图

    原文:使用ArcGIS API for Silverlight + Visifire绘制地图统计图 最近把很久之前做的统计图又拿出来重新做了一遍,感觉很多时候不复习,不记录就真的忘了,时间是最好的稀释 ...