Leetcode 题目整理 climbing stairs
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?
Note: Given n will be a positive integer.
对这种题目开始没有什么思路,借鉴博客http://blog.csdn.net/kenden23/article/details/17377869 给出的递归的思想。每次有两种选择,两种选择之后又是各有两种选择,如此循环,正好是递归求解的问题。
int Solution::climbStairs(int n) {
//直接进行递归
if (n == 1) return 1;
if (n == 2) return 2;
return climbStairs(n - 1) + climbStairs(n - 2);
}
但直接进行递归在leetcode 上的测试没有通过,给的理由是Time Limit Exceeded;
尝试博客中的第二种方法:
共有1级的时候有1种可能,n=1时 res[1]=1;
共有2级的时候有2种可能,n=2时res[2]=2;
共有3级的时候等于最后一步走了1级的可能结果res(2) 加上最后一步走了两级的可能结果res(1) ,即 n=3时res[3]=res(2)+res(1);
所以有循环 res[n]=res(n-1)+res(n-2);
int Solution::climbStairs(int n) {
//利用递归和循环的转换
if (n == 1) return 1;
if (n == 2) return 2;
vector<int> res;
res.push_back(1);
res.push_back(2);
for (int i = 2; i < n ; i++)
{
res.push_back(res.at(i - 1) + res.at(i - 2));
}
return res.back();// .at(n - 1);
}
通过了,在博客中还指出了使用空间问题,确实,在代码执行的过程中只用到最后三个,而且最终的输出也只是最后一个,所以空间完全可以重复利用。
Leetcode 题目整理 climbing stairs的更多相关文章
- [LeetCode] Min Cost Climbing Stairs 爬楼梯的最小损失
On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once you pay ...
- 【LeetCode练习题】Climbing Stairs
Climbing Stairs You are climbing a stair case. It takes n steps to reach to the top. Each time you c ...
- Leetcode之70. Climbing Stairs Easy
Leetcode 70 Climbing Stairs Easy https://leetcode.com/problems/climbing-stairs/ You are climbing a s ...
- 【LeetCode】70. Climbing Stairs 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目大意 题目大意 解题方法 递归 记忆化搜索 动态规划 空间压缩DP 日期 [L ...
- 【一天一道LeetCode】#70. Climbing Stairs
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 You are ...
- LeetCode算法题-Climbing Stairs(Java实现)
这是悦乐书的第159次更新,第161篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第18题(顺位题号是70).你正在爬楼梯,它需要n步才能达到顶峰.每次你可以爬1或2步, ...
- 【LeetCode】070. Climbing Stairs
题目: You are climbing a stair case. It takes n steps to reach to the top. Each time you can either cl ...
- 【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题解之Climbing Stairs
1.题目描述 2.问题分析 使用动态规划. 3.代码 int climbStairs(int n) { ){ return n; } ]; dp[] = ; dp[] = ; dp[] = ; ; i ...
随机推荐
- DEVOPS技术实践_06:sonar与Jenksin集成
代码质量管理平台 一.checkout和打包功能 1.1 gitlab在新建一个文件 后续在写入内容 1.2 Jenkins新建一个任务 两个参数 1.3 流水线配置 copy仓库地址: http:/ ...
- [**P2766** 最长不下降子序列问题](https://www.luogu.org/problemnew/show/P2766)
P2766 最长不下降子序列问题 考虑我们是如何\(dp\)这个\(LIS\)的. 我们是倒着推,设置\(dp(i)\)代表以\(i\)为起点的\(LIS\)是多少.转移太显然了 \[ dp(i)=m ...
- $Poj2083/AcWing118\ Fractal$ 模拟
$AcWing$ $Sol$ 一年前做过差不多的南蛮图腾,当时做出来还是很有成就感的$OvO$ $N<=7$,就是模拟模拟,预处理一下,$over$ $Code$ #include<bit ...
- [02]java数据类型和运算符等知识
00 Java中的注释 为了方便程序的阅读,Java语言允许程序员在程序中写上一些说明性的文字,用来提高程序的可读性,这些文字性的说明就称为注释.注释不会出现在字节码文件中,即Java编译器编译时会跳 ...
- Mysql备份与恢复(2)---逻辑备份
数据库及时备份可以帮助我们在数据库出现异常宕机时及时的使用备份数据进行恢复工作,将因为数据库宕机产生的影响降低到最小.上一篇针对使用xtrabackup工具进行物理备份和数据恢复做了一个详细讲解,本篇 ...
- Docker+Nginx使用流程(笔记)
Docker+Nginx使用流程 本教程依据个人理解并经过实际验证为正确,特此记录下来,权当笔记. 注:基于linux操作系统 # uname -r 查看你当前的内核版本 # yum -y insta ...
- postman传递当前时间戳
有时我们在请求接口时,需要带上当前时间戳这种动态参数,那么postman能不能自动的填充上呢. 1请求动态参数(例如时间戳) 直接在参数值写 {{$timestamp}} 如下: 我们也可以使用pos ...
- Python中url标签使用详解
url标签: 1.在模板中,我们经常要使用一些url,实现页面之间的跳转,比如某个a标签中需要定义href属性.当然如果通过硬编码的方式直接将这个url固定在里面也是可以的,但是这样的话,对于以后进行 ...
- acmPush模块示例demo
感谢论坛版主 马浩川 的分享. 模块介绍: 阿里移动推送(Alibaba Cloud Mobile Push)是基于大数据的移动智能推送服务,帮助App快速集成移动推送的功能,在实现高效.精确.实时 ...
- dp-最长回文串
博客 : http://blog.csdn.net/hao_zong_yin/article/details/72730732 问题描述: 求一个序列中的最长回文串,这个串可以不连续 , 如 { 1 ...