要求

  • 楼梯共有n个台阶,每次上一个台阶或两个台阶,一共有多少种上楼梯的方法?

示例

  • 输入:n=3
  • [1,1,1],[1,2,],[2,1]
  • 输出:n=3

实现

  • 自顶向下(递归)

递归

 1 class Solution {
2
3 private:
4 int calcWays(int n){
5
6 if( n == 0 || n == 1 )
7 return 1;
8
9 return calcWays(n-1) + calcWays(n-2);
10 }
11
12 public:
13 int climbStairs(int n) {
14
15 return calcWays(n);
16 }
17 };

递归+记忆化搜索

 1 class Solution {
2
3 private:
4 vector<int> memo;
5
6 int calcWays(int n){
7
8 if( n == 0 || n == 1 )
9 return 1;
10
11 if( memo[n] == -1 )
12 memo[n] = calcWays(n-1) + calcWays(n-2);
13
14 return memo[n];
15 }
16
17 public:
18 int climbStairs(int n) {
19
20 memo = vector<int>(n+1,-1);
21 return calcWays(n);
22 }
23 };

  • 自底向上(动态规划)

    • 将原问题拆解成若干子问题,同时保存子问题的答案,使得每个问题只求解一次,最终获得原问题的答案

 1 class Solution {
2
3 public:
4 int climbStairs(int n) {
5
6 vector<int> memo(n+1,-1);
7
8 memo[0] = 1;
9 memo[1] = 1;
10 for( int i = 2 ; i <= n ; i ++ )
11 memo[i] = memo[i-1]+memo[i-2];
12 return memo[n];
13 }
14 };

相关

  • 120 Triangle
  • 64 Minimum Path Sum

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

  1. 刷题70. Climbing Stairs

    一.题目说明 题目70. Climbing Stairs,爬台阶(楼梯),一次可以爬1.2个台阶,n层的台阶有几种爬法.难度是Easy! 二.我的解答 类似的题目做过,问题就变得非常简单.首先用递归方 ...

  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. PAT (Advanced Level) Practice 1005 Spell It Right (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1005 Spell It Right (20 分) 凌宸1642 题目描述: Given a non-negative integer N ...

  2. odoo 安装配置

    Linux ubuntu 环境 1.建个python虚环境 也可用其他替代 virtualenv env 2.找个稳定版本下载 wget https://nightly.odoo.com/8.0/ni ...

  3. gRPC在 ASP.NET Core 中应用学习(二)

    前言: 上一篇文章中简单的对gRPC进行了简单了解,并实现了gRPC在ASP.NET Core中服务实现.客户端调用:那么本篇继续对gRPC的4中服务方法定义.其他使用注意点进一步了解学习 一.gRP ...

  4. 【DB宝47】企业知识分享+团队协作神器之Confluence

    目录 一.Confluence简介 二.知识库软件对比 三.快速安装confluence 7.4.6版本 四.confluence基本操作简介 4.1.创建空间(Space) 4.2.配置空间权限 4 ...

  5. 创建线程的方式三:实现Callable接口 --- JDK 5.0新增

    /** * 创建线程的方式三:实现Callable接口. --- JDK 5.0新增 * * * 如何理解实现Callable接口的方式创建多线程比实现Runnable接口创建多线程方式强大? * 1 ...

  6. 【C/C++】malloc和new的区别

    malloc和new的区别 malloc是C语言的内存申请函数.new是C++语言的运算符.所以在.c文件中无法使用new. malloc申请空间时,传递的是size.new申请空间时,传递的是typ ...

  7. 以Aliyun体验机为例,从零搭建LNMPR环境(下)

    使用云服务器搭建 Web 运行环境,尤其是搭建常见的 LNMPR(Linux+Nginx+MySQL+PHP+Redis) 环境,对于开发人员是必备的职场基本技能之一.在这里,借着搭建我的" ...

  8. 几十行代码实现ASP.NET Core自动依赖注入

    在开发.NET Core web服务的时候,我们习惯使用自带的依赖注入容器来进行注入. 于是就会经常进行一个很频繁的的重复动作:定义一个接口->写实现类->注入 有时候会忘了写Add这一步 ...

  9. 它来了!!!有史以来第一个64位Visual Studio(2022)预览版将在今夏发布!

    美国时间2021年4月19日,微软产品研发部一位负责人Amanda Silver在其博客上发布一则<Visual Studio 2022>的消息,表示将在今年(2021年)夏天发布Visu ...

  10. 配置Jupyter环境:安装+补全+美化+常用库

    1 Jupyter简介 Jupyter Notebook是一个交互式笔记本,支持运行40多种编程语言,本质是一个Web应用程序,便于创建和共享文学化程序文档,支持实时代码,数学方程,可视化和Markd ...