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的更多相关文章

  1. 【LeetCode】70. Climbing Stairs 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目大意 题目大意 解题方法 递归 记忆化搜索 动态规划 空间压缩DP 日期 [L ...

  2. 【一天一道LeetCode】#70. Climbing Stairs

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 You are ...

  3. 【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 ...

  4. 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 ...

  5. [leetcode DP]70. Climbing Stairs

    一共有n个台阶,每次跳一个或者两个,有多少种走法,典型的Fibonacii问题 class Solution(object): def climbStairs(self, n): if n<0: ...

  6. LeetCode练题——70. Climbing Stairs

    1.题目 70. Climbing Stairs——Easy You are climbing a stair case. It takes n steps to reach to the top. ...

  7. 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 ...

  8. 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 ...

  9. Leetcode#70. Climbing Stairs(爬楼梯)

    题目描述 假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数. 示例 1: 输入: 2 输出: 2 解 ...

随机推荐

  1. P5496 【模板】回文自动机(PAM)

    做一下强制在线处理即可 #include <cstdio> #include <algorithm> #include <cstring> using namesp ...

  2. [SCOI2016]美味——主席树+按位贪心

    原题戳这里 题解 让异或值最大显然要按位贪心,然后我们还发现加上一个\(x_i\)的效果就是所有\(a_i\)整体向右偏移了,我们对于\({a_i}\)开个主席树,支持查询一个区间中有多少个在\([L ...

  3. Vivado添加coe文件

    直接将.txt文件的后缀改为.coe,并在文件的开头添加如下两行代码即可: memory_initialization_radix=10; memory_initialization_vector=

  4. Redis 4.x RCE 复现学习

    攻击场景: 能够访问远程redis的端口(直接访问或者SSRF) 对redis服务器可以访问到的另一台服务器有控制权 实际上就是通过主从特性来 同步传输数据,同时利用模块加载来加载恶意的用来进行命令执 ...

  5. UTC ISO 8601

    如果时间在零时区,并恰好与协调世界时相同,那么(不加空格地)在时间最后加一个大写字母Z.Z是相对协调世界时时间0偏移的代号.如下午2点30分5秒表示为14:30:05Z或143005Z:只表示小时和分 ...

  6. mybatis之分页查询

    1)StudentDao.java /** * 持久层*/ public class StudentDao { /** * 增加学生 */ public void add(Student studen ...

  7. easuUI之datebox日期选择框

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...

  8. JS原生代码之倒计时抢购

    学到了原声js改变input的disabled的属性值,因为想让倒计时结束的同时,抢购按钮可以被点击.代码为:document.getElementById("buy").disa ...

  9. 012-数据结构-树形结构-哈希树[hashtree]、字典树[trietree]、后缀树

    一.哈希树概述 1.1..其他树背景 二叉排序树,平衡二叉树,红黑树等二叉排序树.在大数据量时树高很深,我们不断向下找寻值时会比较很多次.二叉排序树自身是有顺序结构的,每个结点除最小结点和最大结点外都 ...

  10. php版本:实现过滤掉广告、色情、政治相关的敏感词

    现在网络上还是很乱,尤其充斥着各种广告.色情.政治相关的内容,很明显这是不符合我们国家的法律的,所以为了一个产品能够健康长久的活下去,最好还是采用一定的策略过滤或者提醒用户不要发这种内容.不过说起来容 ...