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 ...
随机推荐
- $Poj1821\ Fence\ $单调队列优化$DP$
Poj Acwing Description 有N块木板等待被M个工匠粉刷,每块木板至多被刷一次.第i个工匠要么不粉刷,要么粉刷包含木块Si的,长度不超过Li的连续的一段木板,每粉刷一块可以得到P ...
- VBA工程密码破解
如何破解VBA密码呢? 见过网上很多关于破解VBA工程密码的方式,最常见的如下这种,但其实对于很多版本是不可行的. 基本都会提示“请先对VBA编码设置一个保护密码...” Sub VBAPassw ...
- 1081 检查密码 (15分)C语言
本题要求你帮助某网站的用户注册模块写一个密码合法性检查的小功能.该网站要求用户设置的密码必须由不少于6个字符组成,并且只能有英文字母.数字和小数点 .,还必须既有字母也有数字. 输入格式: 输入第一行 ...
- 现代主流框架路由原理 hash、history的底层原理
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- echarts更改坐标轴文字颜色及大小
xAxis: { data: anameArr, axisLabel: { show: true, textStyle: { color: '#c3dbff', //更改坐标轴文字颜色 fontSiz ...
- 探究公钥、私钥、对称加密、非对称加密、hash加密、数字签名、数字证书、CA认证、https它们究竟是什么,它们分别解决了通信过程的哪些问题。
一.准备 1. 角色:小白.美美.小黑. 2. 剧情:小白和美美在谈恋爱:小黑对美美求而不得.心生怨念,所以从中作梗. 3. 需求:小白要与美美需通过网络进行通信,联络感情,所以必须保证通信的安全性. ...
- const和volitale
1. const只读变量 const修饰的变量是只读的,本质还是变量 const修饰的局部变量在栈上分配空间(可以通过指针修改) const修饰的全局变量在全局数据区分配空间(指针也修改不了) con ...
- 6.反编译 java---class (字节码文件)---反编译(IDEA):
- sqlserver2008:在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接。
在开始菜单中找到: 进入,点击左侧SQL Server服务, 将SQL Server(MSSQL.....)服务开启, 即可成功连接.
- 深入浅出| java中的clone方法
每天进步一丢丢,连接梦与想 我们还年轻,但这不是你浪费青春的理由 克隆和复制 clone,有人称之为克隆,有人称之为复制,其实都是同一个东西 本文称之为"克隆",毕竟人家方法名叫& ...