题目链接

Climbing Stairs - LeetCode

注意点

  • 注意边界条件

解法

解法一:这道题是一题非常经典的DP题(拥有非常明显的重叠子结构)。爬到n阶台阶有两种方法:1. 从n-1阶爬上 2. 从n-2阶爬上。很容易得出递推式:f(n) = f(n-1)+f(n-2)于是可以得到下面这种最简单效率也最低的解法 —— 递归。

  1. class Solution {
  2. public:
  3. int climbStairs(int n) {
  4. if(n == 0 || n == 1 || n == 2)
  5. {
  6. return n;
  7. }
  8. return climbStairs(n-1)+climbStairs(n-2);
  9. }
  10. };

解法二:思路不变,改为更高效的写法 —— 迭代。时间复杂度O(n)。

  1. class Solution {
  2. public:
  3. int climbStairs(int n) {
  4. vector<int> ans;
  5. int i;
  6. for(i = 0;i <= 2;i++)
  7. {
  8. ans.push_back(i);
  9. }
  10. for(i = 3;i <= n;i++)
  11. {
  12. ans.push_back(ans[i-1]+ans[i-2]);
  13. }
  14. return ans[n];
  15. }
  16. };

解法三:继续优化,可以看出解法二中需要开一个额外的数组来保存过程中计算的值,这些值计算完之后就没用了,所以改用两个变量来替代。时间复杂度O(n),空间复杂度O(1)

  1. class Solution {
  2. public:
  3. int climbStairs(int n) {
  4. if(n == 0||n == 1||n == 2)
  5. {
  6. return n;
  7. }
  8. int a = 2,b = 1,i;
  9. for(i = 0;i < n-2;i++)
  10. {
  11. a = a+b;
  12. b = a-b;
  13. }
  14. return a;
  15. }
  16. };

或者一个更好理解的

  1. class Solution {
  2. public:
  3. int climbStairs(int n) {
  4. if(n == 0||n == 1||n == 2)
  5. {
  6. return n;
  7. }
  8. int a = 2,b = 1,ret,i;
  9. for(i = 0;i < n-2;i++)
  10. {
  11. ret = a+b;
  12. b = a;
  13. a = ret;
  14. }
  15. return ret;
  16. }
  17. };

小结

  • 这道题可以扩展到每次可以走k步,那递推式就变为f(n) = f(n-1) + f(n-2) + ... + f(n-k)

Climbing Stairs - LeetCode的更多相关文章

  1. Min Cost Climbing Stairs - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Min Cost Climbing Stairs - LeetCode 注意点 注意边界条件 解法 解法一:这道题也是一道dp题.dp[i]表示爬到第i层 ...

  2. climbing stairs leetcode java

    问题描述: You are climbing a stair case. It takes n steps to reach to the top. Each time you can either ...

  3. [LeetCode] Climbing Stairs 爬梯子问题

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...

  4. Leetcode: climbing stairs

    July 28, 2015 Problem statement: You are climbing a stair case. It takes n steps to reach to the top ...

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

  6. [LeetCode] Climbing Stairs (Sequence DP)

    Climbing Stairs https://oj.leetcode.com/problems/climbing-stairs/ You are climbing a stair case. It ...

  7. [LeetCode] 746. Min Cost Climbing Stairs 爬楼梯的最小损失

    On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once you pay ...

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

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

随机推荐

  1. Ubuntu解压zip包中文乱码

    解决方法:通过unar 工具解压 步骤一: 安装unar: sudo apt-get install unrar 步骤二: 解压(以test.zip为例):unar test.zip 解压成功,乱码问 ...

  2. MUI的踩坑笔记

    最近在做公司项目的手机端实现,稍微记录下遇到的坑 1.在app开发中,若要使用HTML5+扩展api,必须等plusready事件发生后才能正常使用,mui将该事件封装成了mui.plusReady( ...

  3. PHP XXE漏洞

    PHP xml 外部实体注入漏洞(XXE) 1.环境 PHP 7.0.30Libxml 2.8.0Libxml2.9.0 以后 ,默认不解析外部实体,对于PHP版本不影响XXE的利用 2.原理介绍 X ...

  4. Metasploit拿Shell

    进入metasploit系统 msfconsole Nmap端口扫描 nmap –sV IP(或者域名),如果机器设置有防火墙禁ping,可以使用nmap -P0(或者-Pn) –sV IP(或者域名 ...

  5. Ubuntu下开启mysql远程访问

    1. 开启数据库3306端口 首先,使用如下指令查看3306端口是否对外开放. netstat -an | grep 3306 tcp 0 0 127.0.0.1:3306 0.0.0.0:* LIS ...

  6. 特别好用的eclipse快捷键

    alt+/ 提示 alt+shift+r重命名 alt+shift+j添加文档注释 Ctrl+shift+y小写 Ctrl+shift+x大写 ctrl+shift+f格式化代码(需要取消输入法的简繁 ...

  7. Date 类的使用

    package com.Date.Math; import java.text.ParseException; import java.text.SimpleDateFormat; import ja ...

  8. 第一个Sprint冲刺成果

    组长:李咏江,组员:叶煜稳,谢洪跃,周伟雄 进程:第一个算法功能完成

  9. 04_Java基础语法_第4天(数组)_讲义

    今日内容介绍 1.流程控制语句switch 2.数组 3.随机点名器案例 01switch语句解构 * A:switch语句解构 * a:switch只能针对某个表达式的值作出判断,从而决定程序执行哪 ...

  10. 『编程题全队』Alpha 阶段冲刺博客Day4

    1.每日站立式会议 1.会议照片 2.昨天已完成的工作统计 孙志威: 1.添加团队界面下的看板容器SlotWidget 2.实现SlotWidgets的动态布局管理 3.实现团队/个人界面之间的切换 ...