题目链接

Climbing Stairs - LeetCode

注意点

  • 注意边界条件

解法

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

class Solution {
public:
int climbStairs(int n) {
if(n == 0 || n == 1 || n == 2)
{
return n;
}
return climbStairs(n-1)+climbStairs(n-2);
}
};

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

class Solution {
public:
int climbStairs(int n) {
vector<int> ans;
int i;
for(i = 0;i <= 2;i++)
{
ans.push_back(i);
}
for(i = 3;i <= n;i++)
{
ans.push_back(ans[i-1]+ans[i-2]);
}
return ans[n];
}
};

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

class Solution {
public:
int climbStairs(int n) {
if(n == 0||n == 1||n == 2)
{
return n;
}
int a = 2,b = 1,i;
for(i = 0;i < n-2;i++)
{
a = a+b;
b = a-b;
}
return a;
}
};

或者一个更好理解的

class Solution {
public:
int climbStairs(int n) {
if(n == 0||n == 1||n == 2)
{
return n;
}
int a = 2,b = 1,ret,i;
for(i = 0;i < n-2;i++)
{
ret = a+b;
b = a;
a = ret;
}
return ret; }
};

小结

  • 这道题可以扩展到每次可以走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. 六度空间(MOOC)

    六度空间: “六度空间”理论又称作“六度分隔(Six Degrees of Separation)”理论.这个理论可以通俗地阐述为:“你和任何一个陌生人之间所间隔的人不会超过六个,也就是说,最多通过五 ...

  2. 在Maven上Web项目添加Spring框架

    1. pom.xml添加Spring依赖包 <!-- spring 核心依赖--> <!-- context依赖beans,aop,core,expression;core依赖log ...

  3. Netty源码分析第6章(解码器)---->第2节: 固定长度解码器

    Netty源码分析第六章: 解码器 第二节: 固定长度解码器 上一小节我们了解到, 解码器需要继承ByteToMessageDecoder, 并重写decode方法, 将解析出来的对象放入集合中集合, ...

  4. python - 定时清理ES 索引

    只保留三天 #!/usr/bin/env python3 # -*- coding:utf-8 -*- import os import datetime # 时间转化为字符串 now_time = ...

  5. vmware安装androidx86 (FreeBSD) 系统图解

    有时候自己手机的一些方面限制的因素,我们需要在电脑上装一个“手机”,来完成我们想要做的事情. 安装步骤如下: 首先需要一个ISO系统镜像,下面地址可以提供大量镜像下载: https://zh.osdn ...

  6. 备份win10的驱动程序

    目录 折腾历程 怎么备份驱动 备份的驱动如何使用 关于驱动程序的OS兼容性 驱动程序的其他安装方式 1.折腾历程 从闲鱼上收了一个INSIGNIA的二合一笔记本,w7100,因原装win10性能不行自 ...

  7. init命令详解

    基础命令学习目录首页 1.手动输入命令会执行相关操作   #init 0 - 停机(千万不能把initdefault 设置为0 )   #init 1 - 单用户模式   #init 2 - 多用户, ...

  8. 调试存储过程:ORA-0131 Insufficient privileges

    http://www.cnblogs.com/empty01/p/5568250.html

  9. Bing词典vs有道词典比对测试报告——体验篇之成长性及用户控制权

    成长性: 会记住曾经查询过的单词或例句与有道词典实现基本一样,并无特别亮点. 用户有控制权: 必应词典和有道词典都能实现基本的查询前进和后退.以及无法查找结果,能顺利进行反馈. 我们在输入完单词按下回 ...

  10. java第四次实验报告

    课程:Java程序与设计     班级:1352 姓 名:池彬宁  小组成员: 20135212池彬宁 20135208贺邦 学号:20135212 成绩:             指导教师:娄嘉鹏  ...