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 ...
随机推荐
- Redo与Undo的理解
本文概要本文的原意是一篇个人学习笔记,为了避免成为草草记录一下的流水账,尝试从给人介绍的角度开写.但在整理的过程中,越来越感觉力不从心,一是细节太多了,原以为足够了解的一个小知识点下可能隐藏了很多细节 ...
- ECShop二次开发指南-文件结构(二)
ecshop文件架构说明 注意:因各版权不一,大概参考/* ECShop 2.5.1 的结构图及各文件相应功能介绍 ECShop2.5.1_Beta upload 的目录 ┣ activity.p ...
- 【C++】CCFCSP201803-2碰撞的小球
// // main.cpp // CCFCSP20180318_2_碰撞的小球 // // Created by T.P on 2018/3/24. // Copyright © 2018年 T.P ...
- 「CH2101」可达性统计 解题报告
CH2101 可达性统计 描述 给定一张N个点M条边的有向无环图,分别统计从每个点出发能够到达的点的数量.N,M≤30000. 输入格式 第一行两个整数N,M,接下来M行每行两个整数x,y,表示从x到 ...
- js中getBoundingClientrRect()方法的详解
getBoundingClientRect(): 这个方法返回一个矩形对象,包含四个属性:left.top.right和buttom.分别表示元素各边与页面各边的距离 例如: var boxPosit ...
- 机器学习实战笔记(一)- 使用SciKit-Learn做回归分析
一.简介 这次学习的书籍主要是Hands-on Machine Learning with Scikit-Learn and TensorFlow(豆瓣:https://book.douban.com ...
- 为QLabel增加Clicked信号
QT为QLabel添加Click事件(如果我们使用组件,我们关心的是信号槽:如果我们自定义组件,我们关心的是事件) 其实就是改写了一个函数:mouseReleaseEvent,当在QLabel放开鼠标 ...
- ArcGIS Desktop直连PostgreSQL安装及配置图解(windows)
目录 1 PostgreSQL 11.0安装及配置 2 psqlODBC安装及配置 3 PostGIS安装及配置 4 pgAdmin4使用入门 5 空间数据导入 5.1 将PostgreSQL的bin ...
- ArcEngine 创建线要素图层
在创建要素图层的时候,默认的几何类型是Polygon: Dim objectClassDescription As IObjectClassDescription = New FeatureClass ...
- 《【面试突击】— Redis篇》--Redis都有哪些数据类型?分别在哪些场景下使用比较合适?
能坚持别人不能坚持的,才能拥有别人不能拥有的.关注编程大道公众号,让我们一同坚持心中所想,一起成长!! <[面试突击]— Redis篇>--Redis都有哪些数据类型?分别在哪些场景下使用 ...