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?

DFS

对于n = 3的情况:

有3种途径

(1, 1, 1)、(1, 2)、(2, 1)

这里(1,2)与(2,1)是两种。

#include <iostream>
#include <set>
#include <map>
#include <vector>
#include <sstream>
#include <string>
#include <algorithm>
#include <bitset>
using namespace std; class Solution {
public:
void dfs(int n)
{
if (n < 0)
{
return;
}
else if(n == 0)
{
count++;
}
else
{
dfs(n - 1);
dfs(n - 2);
}
} int climbStairs(int n) { if (m.count(n))
{
return m[n];
}
else
{
count = 0;
dfs(n);
m[n] = count;
return count;
}
}
private:
int count;
map<int, int>m;
}; int main()
{
Solution s;
for (int i = 1; i <= 20; ++i)
{
cout << i << ":" << s. climbStairs(i) << endl;
}
return 0;
}

毫无疑问,超时了

1:1
2:2
3:3
4:5
5:8
6:13
7:21
8:34
9:55
10:89
11:144
12:233
13:377
14:610
15:987
16:1597
17:2584
18:4181
19:6765
20:10946
[Finished in 2.0s]

观察规律:

X[i] = X[i - 1] + X[i - 2](i > 2)

那么可以采用记忆化搜索,也就是将中间的结果保存下

#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <string>
#include <sstream>
#include <cstring>
using namespace std; class Solution {
public:
Solution()
{
memset(num, 0, sizeof(num));
num[1] = 1;
num[2] = 2;
} int dfs(int n)
{
if (num[n] != 0)
{
return num[n];
}
return (num[n] = dfs(n - 1) + dfs(n - 2)); } int climbStairs(int n) {
if (num[n] == 0)
{
dfs(n);
}
return num[n];
}
private:
int num[10000];
}; int main()
{
Solution s;
cout << s.climbStairs(44);
return 0;
}

70. Climbing Stairs QuestionEditorial Solution的更多相关文章

  1. 377. Combination Sum IV 70. Climbing Stairs

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

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

  3. Leetcode之70. Climbing Stairs Easy

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

  4. 刷题70. Climbing Stairs

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

  5. LeetCode练题——70. Climbing Stairs

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

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

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

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

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

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

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

随机推荐

  1. 洛谷P1029 最大公约数和最小公倍数问题 题解

    题目链接:https://www.luogu.com.cn/problem/P1029 题目描述 输入 \(2\) 个正整数 \(x_0,y_0(2 \le x_0 \lt 100000,2 \le ...

  2. 原生js获取下拉框下标

    // 获取下拉框所选下标 传入下拉框的id function getselectscheckitemindex (idStr) { let o = document.getElementById(id ...

  3. 小小知识点(十七)——对数形式功率(dBm)与非对数形式功率(w)之间的换算关系

    摘自https://blog.csdn.net/shij19/article/details/52946454 dBm 物理含义是:一个表示功率绝对值的值(也可以认为是以1mW功率为基准的一个比值) ...

  4. @Configuration结合@Bean实现对象的配置

    @Configuration结合@Bean实现对象的配置 前提:最近项目中需要做支付接口,支付宝以及微信支付,本文并不介绍如何写支付接口,而是通过这个示例讲解配置应该怎么写,项目中使用的是Kotlin ...

  5. 【Java基础总结】字符串

    1. java内存区域(堆区.栈区.常量池) 2. String length() //长度 //获取子串位置 indexOf(subStr) lastIndexOf(subStr) //获取子串 c ...

  6. vue将接口返回的日期实时转换为几分钟前、几小时前、几天前

    项目开发中,各种需求都会遇到,有些需求很合理,也好实现,有些需求不能说不合理,就是太麻烦,就比如类似标题所描述这种的需求,你不能说它是不合理的需求,因为很多论坛或微博.朋友圈.QQ空间之类的这种效果还 ...

  7. 搞定SpringBoot多数据源(1):多套源策略

    目录 1. 引言 2. 运行环境 3. 多套数据源 3.1 搭建 Spring Boot 工程 3.1.1 初始化 Spring Boot 工程 3.1.2 添加 MyBatis Plus 依赖 3. ...

  8. echarts更改折线图区域颜色、折线颜色、折点颜色

    series : [ { name:'订单流入总数', type:'line', stack: '总量', areaStyle: { normal: { color: '#8cd5c2' //改变区域 ...

  9. No property andp found for type String! Traversed path: CmsPage.siteId.

    Respository没有找到该参数 1)由该参数,函数名写错,不符合JPA规范 2)实体类没有该参数

  10. 一个由"2020年1月7日 京东出现的重大 Bug 漏洞"引起的思考...

    2020年1月7日,京东由于优惠券设置错误,导致大量产品以0元或者超低价成交,并且发货.网传小家电被薅24万件,损失损失金额高达7000多万.很多网友表示收到货了,在网上晒出到货截图.下面为购买截图: ...