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. 毕业两年半,入手人生第一款macbook pro

    当程序员入手第一款macbook 大家好,我是灰大狼,你们可以叫我灰狼.大狼.甚至是小灰灰. 接下来我主要跟大家分享下作为程序员的我,刚入手一款mac的使用心得. 背景 做程序员三年了,一直用的都是w ...

  2. vmware 14 激活码

    VMware虚拟机已升级至14版本,之前的12版本的秘钥已经无法使用,在此分享一下VMware Workstation 14永久激活密钥: CG54H-D8D0H-H8DHY-C6X7X-N2KG6 ...

  3. C语言关键字const作用及其应用

    只要学过C语言的,都有知道const这个关键字,知道是用来定义常量的,如果一个变量被const修饰,那么它的值就不能再被改变,那么还有什么其他作用呢? 一.const常用作用 1.修饰局部变量 con ...

  4. Linux下扫描服务器IP地址是否冲突(arp-scan)

    部署服务突然发现,连接的服务器断开了,因为服务器用户名密码是一样的,所以重新连接后,发现文件变了,跟之前不一样. 猜想是不是ip地址冲突了,两次连接的服务器不同. 网上查找资料说可以用工具扫描.工具: ...

  5. 使用SqlDependency实时监听SQL server数据库变化并执行事件

    sql server设置:ALTER DATABASE <DatabaseName> SET ENABLE_BROKER;语句让相应的数据库启用监听服务,以便支持SqlDependency ...

  6. 关闭Win10 445端口

    这次勒索病毒利用了NSA黑客工具包中的“永恒之蓝”0day漏洞,通过电脑445端口(文件共享<普通用户基本用不到这个端口,可以关闭>)在内网进行蠕虫式感染传播,为了防止内网感染,建议用户关 ...

  7. OpenLayers4 隐藏(hide)Feature

    需求: 需要将同一图层的要素进行分类显示和隐藏(类似于图层控制) 方法: 使用setStyle方法将Feature的样式设置为null. 环境: win10.google chrome.OL 4.3 ...

  8. Python - 线性回归(Linear Regression) 的 Python 实现

    背景 学习 Linear Regression in Python – Real Python,前面几篇文章分别讲了"regression怎么理解","线性回归怎么理解& ...

  9. 通过HttpClient的方式去Curd数据⭐⭐⭐⭐

    在网上看博客的时候,看到这系列的文章,别特帮,强烈推荐 里面有一章节是通过HttpClient的方法去更新数据的,新颖,记录下. ⭐⭐⭐1:创建一个Model数据模型 这个类创建一个数据对象,Http ...

  10. mysql累加、累减

    累加 先上表结构: CREATE TABLE `abc` ( `jidu` ) NOT NULL AUTO_INCREMENT, `jine` ) DEFAULT NULL, PRIMARY KEY ...