Leetcode之70. Climbing Stairs Easy
Leetcode 70 Climbing Stairs Easy
https://leetcode.com/problems/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.
Example 1:
- Input: 2
- Output: 2
- Explanation: There are two ways to climb to the top.
- 1. 1 step + 1 step
- 2. 2 steps
Example 2:
- Input: 3
- Output: 3
- Explanation: There are three ways to climb to the top.
- 1. 1 step + 1 step + 1 step
- 2. 1 step + 2 steps
- 3. 2 steps + 1 step
分析:
爬台阶这道题印象深刻,为什么呢,tecent春招电话面考我了,但是年幼无知的我语无伦次,只说了用动态规划。当时旁边没有笔、纸,空荡荡的走廊,在这种情境下,怎么做这道题呢?
首先,逆向思考:要想爬上n_th台阶,那么有两种方式:①(n - 2)_th爬两个台阶;②(n - 1)_th爬一个台阶。也就是ways(n-2) + ways(n-1)即为最后的攀登方式总数。由此可以想到用动规的思想来解这道题。
其次,正向思考:
- 爬第一个台阶,一种方式—— 1step
- 爬第二个台阶,两种方式—— 1step+1step 或 2step
- 爬第三个台阶,想一下怎么利用前两个结果?哈哈,不就是2 + 1吗?!这样话,问题就迎刃而解,对得起Easy这个难度。
- class Solution {
- public:
- int climbStairs(int n) {
- int preTwoStep = ;
- int preOneStep = ;
- int ways = ;
- if (n == || n == )
- return n == ? : ;
- else {
- for (int i = ; i <= n; ++i) {
- ways = preTwoStep + preOneStep;
- preTwoStep = preOneStep;
- preOneStep = ways;
- }
- }
- return ways;
- }
- };
这道题可能在有纸或现场面试的情况下,即使当时比现在更菜,但做出来的概率应该还是很大的,但无奈是电话面试。但是,现在的我并不这么想。这道题是第70题,没有说是某300或500,并且还是一道easy,面试官对我已经很仁慈了;即使这样,我依然做不出来,甚至没有思路,说明我根本没刷到这道题,足以说明我刷题这方面能力欠缺,如果我是面试官,我也不敢要。这告诫我,如果你真的很重视面试,如果你真的想拿offer,那么就不要限于想,就是干!希望在努力的你也一样!
Leetcode之70. Climbing Stairs Easy的更多相关文章
- 【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】70 - Climbing Stairs
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- 70. Climbing Stairs(easy, 号称 Dynamic Programming 天下第一题)
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- [leetcode DP]70. Climbing Stairs
一共有n个台阶,每次跳一个或者两个,有多少种走法,典型的Fibonacii问题 class Solution(object): def climbStairs(self, n): if n<0: ...
- 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 746. Min Cost Climbing Stairs(easy understanding dp solution)
leetcode 746. Min Cost Climbing Stairs(easy understanding dp solution) On a staircase, the i-th step ...
- 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 解 ...
随机推荐
- [NOI2016]循环之美——结论+莫比乌斯反演
原题链接 好妙的一道神仙题 题目大意 让你求在\(k\)进制下,\(\frac{x}{y}\)(\(x\in [1,n],y\in [1,m]\))中有多少个最简分数是纯循环小数 SOLUTION 首 ...
- LINUX查看内存使用情况 free
# free 显示结果如下: Mem:表示物理内存统计 total 内存总数 8057964KB used 已使用的内存 7852484KB free 空闲的内存数 205480KB shared 当 ...
- C#新增按钮
代码亲测可用,似乎不需要“ADD”,如下:form_load段:for (int i = 0; i < 10; i++){btn = new Button();btn.Parent = this ...
- Windows 7 [Web应用程序项目***已配置为使用IIS。无法访问IIS元数据库,您没有足够的特权访问计算机上的IIS网站]
如下所示,我最近也遇到这个类似的错误(Win7 + IIS7.0),最后如下图解决 系统用户权限问题, 通过管理员运行 即可正常使用.
- 错误调试以及debug的使用
/*定义 .search 搜索*/ $.fn.UiSearch=function(){ var ui=$(this); //任何地方都可以使用断点调试:debugger; //调试时,可以在控制台输入 ...
- C语言--变量
unsigned int 和 int 对计算机来讲没有区别, 只有在输出的时候, 计算机根据%d 和 %u 判断是否有符号位. %d 输出有符号的整数, %u 输出无符号整数. 无符号的整数比有符号的 ...
- hdu 5869 Different GCD Subarray Query BIT+GCD 2016ICPC 大连网络赛
Different GCD Subarray Query Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K ( ...
- Transformer模型总结
Transformer改进了RNN最被人诟病的训练慢的缺点,利用self-attention机制实现快速并行. 它是由编码组件.解码组件和它们之间的连接组成. 编码组件部分由一堆编码器(6个 enco ...
- maven创建可执行jar包项目的配置
<build> <plugins> <plugin> <artifactId>maven-shade-plugin</artifactId> ...
- django 快速实现登陆,接着注册的项目写(五)
1.改项目的urls.py from django.conf.urls import url,include from django.contrib import admin admin.autodi ...