刷题70. Climbing Stairs
一、题目说明
题目70. Climbing Stairs,爬台阶(楼梯),一次可以爬1、2个台阶,n层的台阶有几种爬法。难度是Easy!
二、我的解答
类似的题目做过,问题就变得非常简单。首先用递归方法计算:
class Solution{
public:
int climbStairs(int n){
if(n==1) return 1;
if(n==2) return 2;
return climbStairs(n-1) + climbStairs(n-2);
}
};
非常不好意思,Time Limit Exceeded。
那就用dp算法吧:
class Solution{
public:
int climbStairs(int n){
if(n==1) return 1;
if(n==2) return 2;
vector<int>dp;
dp.push_back(1);
dp.push_back(2);
for(int i=2;i<n;i++){
dp.push_back(dp[i-1]+dp[i-2]);
}
return dp[n-1];
}
};
性能:
Runtime: 4 ms, faster than 55.03% of C++ online submissions for Climbing Stairs.
Memory Usage: 8.4 MB, less than 51.47% of C++ online submissions for Climbing Stairs.
三、优化措施
不优化了!
刷题70. Climbing Stairs的更多相关文章
- [刷题] 70 Climbing Stairs
要求 楼梯共有n个台阶,每次上一个台阶或两个台阶,一共有多少种上楼梯的方法? 示例 输入:n=3 [1,1,1],[1,2,],[2,1] 输出:n=3 实现 自顶向下(递归) 递归 1 class ...
- LeetCode练题——70. Climbing Stairs
1.题目 70. Climbing Stairs——Easy You are climbing a stair case. It takes n steps to reach to the top. ...
- Leetcode之70. Climbing Stairs Easy
Leetcode 70 Climbing Stairs Easy https://leetcode.com/problems/climbing-stairs/ You are climbing a s ...
- 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 解 ...
- 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 (爬楼梯) 解题思路和方法
Climbing Stairs You are climbing a stair case. It takes n steps to reach to the top. Each time you ...
- [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 ...
随机推荐
- jQuery实现隔行变色、悬停变色 ( CSS3伪类选择器:nth-child() )
<title>实现隔行变色</title> <script src="Js/jquery-1.8.0.min.js" type="text/ ...
- python接口自动化之fiddler使用(二)
1.快捷设置,自定义会话框,查看get和post请求 (1)鼠标放在#后面,右键 (2)选择Customize columns (3)选择Miscellaneous (4)选择RequestMetho ...
- 洛谷P1042 乒乓球
https://www.luogu.org/problem/P1042 #include<bits/stdc++.h> using namespace std; ]; int w,l; i ...
- ReLU(inplace=True),这里的inplace=true的意思
ReLU(inplace=True),这里的inplace=true的意思 待办 inplace=True means that it will modify the input directly, ...
- maven scope 的作用
一: 1.Maven中的依赖作用范围概述 Maven中使用 scope 来指定当前包的依赖范围和依赖的传递性.常见的可选值有:compile, provided, runtime, test, sys ...
- EF中的查询方法
1.Linq to Entity(L2E)查询 默认返回IQueryable类型 2.原生SQL查询和操作 ①DbSet.SqlQuery()和Database.SqlQuery() 返回DbSqlQ ...
- 自制yum源离线安装ansible
适应场景 在实际生产环境中,服务器往往是不能访问互联网,如果简单的下载ansible源码安装,会碰到缺少各种依赖包的问题,因此,推荐制作yum源,然后使用yum安装ansible. 实验环境 模拟可以 ...
- dijkstra堆优化板子
咕咕咕. #include<queue> #include<cstdio> #include<cstring> #include<algorithm> ...
- mini-batch是什么 以及dataloader的作用
mini-batch是什么 以及dataloader的作用 待办 我们在训练神经网络时,使用的是mini-batch(一次输入多张图片),所以我们在使用一个叫DataLoader的工具为我们将5000 ...
- 题解【洛谷P1074】[NOIP2009]靶形数独
题面 题解 一开始写了一个朴素的数独,无任何剪枝优化,得到了\(55\)分的好成绩. 就是这道题加一个计算分数. 代码如下(\(\mathrm{55\ pts}\)): /************** ...