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的更多相关文章
- Min Cost Climbing Stairs - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Min Cost Climbing Stairs - LeetCode 注意点 注意边界条件 解法 解法一:这道题也是一道dp题.dp[i]表示爬到第i层 ...
- climbing stairs leetcode java
问题描述: You are climbing a stair case. It takes n steps to reach to the top. Each time you can either ...
- [LeetCode] 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
July 28, 2015 Problem statement: You are climbing a stair case. It takes n steps to reach to the top ...
- [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 (Sequence DP)
Climbing Stairs https://oj.leetcode.com/problems/climbing-stairs/ You are climbing a stair case. It ...
- [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 ...
- [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] 70. Climbing Stairs 爬楼梯
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
随机推荐
- Ubuntu解压zip包中文乱码
解决方法:通过unar 工具解压 步骤一: 安装unar: sudo apt-get install unrar 步骤二: 解压(以test.zip为例):unar test.zip 解压成功,乱码问 ...
- MUI的踩坑笔记
最近在做公司项目的手机端实现,稍微记录下遇到的坑 1.在app开发中,若要使用HTML5+扩展api,必须等plusready事件发生后才能正常使用,mui将该事件封装成了mui.plusReady( ...
- PHP XXE漏洞
PHP xml 外部实体注入漏洞(XXE) 1.环境 PHP 7.0.30Libxml 2.8.0Libxml2.9.0 以后 ,默认不解析外部实体,对于PHP版本不影响XXE的利用 2.原理介绍 X ...
- Metasploit拿Shell
进入metasploit系统 msfconsole Nmap端口扫描 nmap –sV IP(或者域名),如果机器设置有防火墙禁ping,可以使用nmap -P0(或者-Pn) –sV IP(或者域名 ...
- Ubuntu下开启mysql远程访问
1. 开启数据库3306端口 首先,使用如下指令查看3306端口是否对外开放. netstat -an | grep 3306 tcp 0 0 127.0.0.1:3306 0.0.0.0:* LIS ...
- 特别好用的eclipse快捷键
alt+/ 提示 alt+shift+r重命名 alt+shift+j添加文档注释 Ctrl+shift+y小写 Ctrl+shift+x大写 ctrl+shift+f格式化代码(需要取消输入法的简繁 ...
- Date 类的使用
package com.Date.Math; import java.text.ParseException; import java.text.SimpleDateFormat; import ja ...
- 第一个Sprint冲刺成果
组长:李咏江,组员:叶煜稳,谢洪跃,周伟雄 进程:第一个算法功能完成
- 04_Java基础语法_第4天(数组)_讲义
今日内容介绍 1.流程控制语句switch 2.数组 3.随机点名器案例 01switch语句解构 * A:switch语句解构 * a:switch只能针对某个表达式的值作出判断,从而决定程序执行哪 ...
- 『编程题全队』Alpha 阶段冲刺博客Day4
1.每日站立式会议 1.会议照片 2.昨天已完成的工作统计 孙志威: 1.添加团队界面下的看板容器SlotWidget 2.实现SlotWidgets的动态布局管理 3.实现团队/个人界面之间的切换 ...