70. Climbing Stairs QuestionEditorial Solution
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的更多相关文章
- 377. Combination Sum IV 70. Climbing Stairs
back function (return number) remember the structure class Solution { int res = 0; //List<List< ...
- 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 ...
- Leetcode之70. Climbing Stairs Easy
Leetcode 70 Climbing Stairs Easy https://leetcode.com/problems/climbing-stairs/ You are climbing a s ...
- 刷题70. Climbing Stairs
一.题目说明 题目70. Climbing Stairs,爬台阶(楼梯),一次可以爬1.2个台阶,n层的台阶有几种爬法.难度是Easy! 二.我的解答 类似的题目做过,问题就变得非常简单.首先用递归方 ...
- LeetCode练题——70. Climbing Stairs
1.题目 70. Climbing Stairs——Easy You are climbing a stair case. It takes n steps to reach to the top. ...
- 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 ...
- Leetcode#70. Climbing Stairs(爬楼梯)
题目描述 假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数. 示例 1: 输入: 2 输出: 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 ...
- leetCode 70.Climbing Stairs (爬楼梯) 解题思路和方法
Climbing Stairs You are climbing a stair case. It takes n steps to reach to the top. Each time you ...
随机推荐
- 洛谷$P$2522 $Problem\ b\ [HAOI2011]$ 莫比乌斯反演
正解:莫比乌斯反演 解题报告: 传送门! 首先看到这个显然就想到莫比乌斯反演$QwQ$? 就先瞎搞下呗$QwQ$ $gcd(x,y)=k$,即$gcd(\left \lfloor \frac{x}{k ...
- $NOIp$普及组做题记录
\([NOIp2014]\) 螺旋矩阵 \(Sol\) 直接模拟,一次走一整行或者一整列.复杂度\(O(n)\). \(Code\) #include<bits/stdc++.h> #de ...
- ObserverPattern(观察者模式)-----Java/.Net
当对象间存在一对多关系时,则使用观察者模式(Observer Pattern).比如,当一个对象被修改时,则会自动通知它的依赖对象.观察者模式属于行为型模式
- Mysql唯一索引线上故障记录
问题现象: Mysql插入一条数据时,未指定自增键的值却报错:自增键重复,无法插入! 执行SQL INSERT INTO `test`.`test_sort`(`id`, `name`, `age`, ...
- DNS自述:我是如何为域名找到家的
对于互联网一代的我们,一出生就学会使用电脑.当我们对着浏览器地址栏输入www.baidu.com的时候,百度的首页就出现在面前.但你可曾想过,为什么我们输入www.baidu.com就可以弹出百度首页 ...
- Mysql 性能优化Explain详解
explain 功能我们在日常使用中,使用慢查询找到执行时间比较久的查询,然后使用SHOW STATUS.SHOW PROFILE.和explain做单条语句的分析.使用explain关键字可以模拟优 ...
- PHP-FPM 远程代码执行漏洞(CVE-2019-11043)的简单复现学习
1.概述 漏洞主要由于 PHP-FPM 中 sapi/ fpm/ fpm/ fpm_main.c 文件内的 env_path_info 下溢导致,攻击者可以使用换行符 %0a 破坏 Nginx 中 f ...
- axios中请求传值方式
日常开发中与后端联调,可能需要的数据不同,所传值也有所不同 1.如果是data方式,设置请求头为:并且直接返回data就可以 raw axios.defaults.headers['Content- ...
- vue实现checked 全选功能
记录一下 module.data = { result: {}, items: [] //初始化全选按钮, 默认不选 ,isCheckedAll: false};module.vue = new V ...
- python 装饰器-初识
一.装饰器的形成过程 1.函数无参数,无返回值 import time def f1(): # 无参数,无返回值 time.sleep(1) print("Hello, World!&quo ...