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 ...
随机推荐
- vue学习笔记(六)表单输入绑定
前言 在上一章vue学习笔记(四)事件处理器这一篇博客的内容中,我们已经了解vue是如何绑定事件的,而本篇博客主要讲解的是vue中表单输入的绑定,通常我们自己提交信息的时候都是通过表单将信息到服务器的 ...
- 洛谷P1248 加工生产调度 贪心
正解:贪心 解题报告: 传送门$QwQ$ $umm$直接看可能比较难想,可以先考虑另一个题? 有$n$个小怪,每打一只小怪会扣$a_i$的血,打完之后会回升$b_i$的血,问至少要多少血量才能使全程血 ...
- 安装Docker Machine
什么是Docker Machine Docker Machine是Docker官方编排项目之一,由Go语言实现,负责在多种平台上快速安装Docker环境,Github项目主页 它支持Linux.Mac ...
- CommandPattern(命令模式)-----Java/.Net
命令模式(Command Pattern)是一种数据驱动的设计模式,它属于行为型模式.请求以命令的形式包裹在对象中,并传给调用对象.调用对象寻找可以处理该命令的合适的对象,并把该命令传给相应的对象,该 ...
- 2020了你还不会Java8新特性?(六)Stream源码剖析
Stream流源码详解 节前小插曲 AutoCloseable接口: 通过一个例子 举例自动关闭流的实现. public interface BaseStream<T, S extends Ba ...
- 调试排错 - Java问题排查:Linux命令
本文原创,更多内容可以参考: Java 全栈知识体系.如需转载请说明原处. Java 在线问题排查主要分两篇:本文是第一篇,通过linux常用命令排查.@pdai 文本操作 文本查找 - grep g ...
- react项目使用antd
在开始实践之前要确保搭建React单页面开发环境,如果还没有搭建好开发环境的朋友请移步到如何搭建React单页面开发环境. 首先在命令行模式下创建一个React项目(项目名使用小写字母命名):(win ...
- JavaScript-EventLoop-事件循环
2020-01-11 EventLoop-事件循环 一.学习事件循环之前,先学习几个英语词组 EventLoop 事件循环 Event Queue 事件队列 Event Table 事件表macro- ...
- file_get_contents函数获取不到数据的一种情况
问题: file_get_contents($url) 获取不到数据,尽管URL地址正确,函数使用正确.如下代码 $url = "https://www.baidu.com"; ...
- Android/Unity大乱斗-完整双方集成交互指南
这是一个很长很长的story!-芝麻粒儿创作 开篇 源码地址:GitHub 本文目的,将Unity集成到Android端,学完本文后你可以做到 Android任意布局加载Unity 3D场景 任意操作 ...