一、题目说明

题目70. Climbing Stairs,爬台阶(楼梯),一次可以爬1、2个台阶,n层的台阶有几种爬法。难度是Easy!

二、我的解答

类似的题目做过,问题就变得非常简单。首先用递归方法计算:

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

非常不好意思,Time Limit Exceeded

那就用dp算法吧:

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

性能:

Runtime: 4 ms, faster than 55.03% of C++ online submissions for Climbing Stairs.
Memory Usage: 8.4 MB, less than 51.47% of C++ online submissions for Climbing Stairs.

三、优化措施

不优化了!

刷题70. Climbing Stairs的更多相关文章

  1. [刷题] 70 Climbing Stairs

    要求 楼梯共有n个台阶,每次上一个台阶或两个台阶,一共有多少种上楼梯的方法? 示例 输入:n=3 [1,1,1],[1,2,],[2,1] 输出:n=3 实现 自顶向下(递归) 递归 1 class ...

  2. LeetCode练题——70. Climbing Stairs

    1.题目 70. Climbing Stairs——Easy You are climbing a stair case. It takes n steps to reach to the top. ...

  3. Leetcode之70. Climbing Stairs Easy

    Leetcode 70 Climbing Stairs Easy https://leetcode.com/problems/climbing-stairs/ You are climbing a s ...

  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. 377. Combination Sum IV 70. Climbing Stairs

    back function (return number) remember the structure class Solution { int res = 0; //List<List< ...

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

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

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

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

随机推荐

  1. C# asp.net 配置文件连接sql 数据库

    先引用 using System.Configuration;//配置文件using System.Data.SqlClient; 我这里使用的是SqlServer 2008  sa 用户 密码也为s ...

  2. Python发带附件的邮件

    python实现发送带附件的邮件 import smtplib from email.mime.text import MIMEText from email.mime.multipart impor ...

  3. opencv3.2.0+opencv_contrib-3.2.0+vs2015相关文件的配置

    包含目录:E:\opencvcontrib\opencv\sources\build\install\include\opencv E:\opencvcontrib\opencv\sources\bu ...

  4. linux 关键字搜索文件

    在www目录下查找包含关键字’summer’,且后缀名为php文件的操作命令 find /www/ -name '*.php' |xargs grep '/Summer'

  5. PHP csv文件30w+数据导入mysql数据库

    <?php class Add { public function data() { ini_set('memory_limit', '-1'); //PHP内存设置 $handle=fopen ...

  6. PHP毫秒

    PHP毫秒   php的毫秒是没有默认函数的,但提供了一个microtime()函数,该函数返回包含两个元素,一个是秒数,一个是小数表示的毫秒数,借助此函数,可以很容易定义一个返回毫秒数的函数,例如: ...

  7. Wannafly Camp 2020 Day 2C 纳新一百的石子游戏

    为什么为了这么个简单题发博客呢? 因为我又因为位运算运算符优先级的问题血了 #include <bits/stdc++.h> using namespace std; #define in ...

  8. c++指针实例

    #include <iostream> using namespace std; int main () { ; // 实际变量的声明 int* ip; // 指针变量的声明 ip = & ...

  9. MySql -- default 默认约束

    常用数据库约束: 一.default 默认约束: 二.not null:非空约束,指定某列不为NULL: 三.unique:唯一约束,指定某列和几列组合的数据不能重复: 四.primary key:主 ...

  10. IntelliJ IDEA 2017.3来自百度----idea原生快捷键

    常用 Ctrl+R  查找加替换 Ctrl+Alt+回车 从当前行,向上加一行 Shift+回车 从当前行,向下加下一行 Ctrl+Alt+L 格式化代码 Ctrl+/ // Ctrl+Shift+/ ...