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技术实践_16:使用Centos容器作为salve的报错offline的问题
上一篇创建了一个centos的容器,而且已经安装了openssh [root@node6 ~]# docker ps -a f2320c5d3c54 centos minutes ago Exited ...
- fastdfs基本安装流程和集成springboot总结
FastDFS介绍 1.简介 FastDFS 是一个开源的高性能分布式文件系统(DFS). 它的主要功能包括:文件存储,文件同步和文件访问,以及高容量和负载平衡.主要解决了海量数据存储问题,特别适合以 ...
- [gitHub实践] 实践记录
[gitHub实践] 实践记录 版权2019.4.19更新 Q1:本地仓库和远程仓库连接提示输入用户名密码 本地仓库和远程仓库连接有两种方式 本地初始化建立一个仓库,远程也建立了一个仓库 本地建立仓库 ...
- centos 7.3 服务器环境搭建——MySQL 安装和配置
centos 7.3 服务器环境搭建——MySQL 安装和配置服务器信息如下:服务器:阿里云系统 centos 7.3 (阿里云该版本最新系统)mysql版本:5.7.18 (当前时间最新版本)连接服 ...
- 用python做推荐系统(一)
一.简介: 推荐系统是最常见的数据分析应用之一,包含淘宝.豆瓣.今日头条都是利用推荐系统来推荐用户内容.推荐算法的方式分为两种,一种是根据用户推荐,一种是根据商品推荐,根据用户推荐主要是找出和这个用户 ...
- MyBatis项目实战 快速将MySQL转换成Oracle语句
一.前言 因项目需求,小编要将项目从mysql迁移到oracle中 ~ 之前已经完成 数据迁移 (https://zhengqing.blog.csdn.net/article/details/103 ...
- 【转】python中查询某个函数的使用方法
使用help(),例查询sum函数的用法 使用官方文档: 1)打开python的IDLE: 2)点击help,选择python doc(这是python的官方文档,或者你也可以直接按f1键) 3)在调 ...
- C# 把文件夹下所有文件添加到集合中
private List<string> FindFile(string Path) { List<string> list=new List<string>(); ...
- Spring注解:InitBinder
注解 InitBinder 是用来初始化绑定器Binder的,而Binder是用来绑定数据的,换句话说就是将请求参数转成数据对象. @InitBinder用于在@Controller中标注于方法,表示 ...
- vue将接口返回的日期实时转换为几分钟前、几小时前、几天前
项目开发中,各种需求都会遇到,有些需求很合理,也好实现,有些需求不能说不合理,就是太麻烦,就比如类似标题所描述这种的需求,你不能说它是不合理的需求,因为很多论坛或微博.朋友圈.QQ空间之类的这种效果还 ...