[leetcode] 14. Climbing Stairs
这道题leetcode上面写着是DP问题,问题是我一开始写了个简单的递归结果直接超时,所以没办法只好拿迭代来做了。题目如下:
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?
这个基本就可以很简单的做成递归。如果楼层是1,那么返回1;如果是2的话,返回2;如果是大于2的n,就可以分为两条线,一条是看作n-1的楼层和1层楼,一条是看作n-2的楼层和2层楼。写成递归就是F(n)=F(n-1) + F(n-2)。就是将这两条线路相加总数,从结果上看就是斐波那契数列。
从递归的角度来写就是这样
class Solution {
public:
int climbStairs(int n)
{
if (n == 1)
{
return 1;
}
if (n == 2)
{
return 2;
}
return climbStairs(n - 1) + climbStairs(n - 2);
}
};
当然这样的话时间消耗非常夸张,在输入44后足足等了快20秒。所以为了降低消耗只能写成了迭代,如下:
class Solution {
public:
int climbStairs(int n)
{
if (n == 1)
{
return 1;
}
if (n == 2)
{
return 2;
}
int aspros = 1;
int deferos = 2;
int star = 0;
for (int i = 0; i < n-2; i++)
{
star = aspros + deferos;
aspros = deferos;
deferos = star;
}
return star;
}
};
通过。
[leetcode] 14. Climbing Stairs的更多相关文章
- [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 ...
- leetCode 70.Climbing Stairs (爬楼梯) 解题思路和方法
Climbing Stairs You are climbing a stair case. It takes n steps to reach to the top. Each time you ...
- 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 解 ...
- LN : leetcode 70 Climbing Stairs
lc 70 Climbing Stairs 70 Climbing Stairs You are climbing a stair case. It takes n steps to reach to ...
- [LeetCode OJ]-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(爬楼梯)(动态规划)(*)
翻译 你正在爬一个楼梯. 它须要n步才干究竟顶部. 每次你能够爬1步或者2两步. 那么你有多少种不同的方法爬到顶部呢? 原文 You are climbing a stair case. It tak ...
- 【leetcode】Climbing Stairs
题目简述: You are climbing a stair case. It takes n steps to reach to the top. Each time you can either ...
随机推荐
- C# split分割多个字符
string[] myAgent = agentInfo.Split(new string[] { "$#$" }, StringSplitOptions.None);
- Asp.net中FileUpload控件实现图片上传并带预览显示
单一图片上传——“选择”+“上传”,.NET默认模式: 1.实现原理: 采用FileUpload控件默认的使用方式,先由“选择”按钮选择图片,然后单击“上传”按钮完成上传,并可在“上传”按钮的 ...
- 88. Merge Sorted Array 后插
合并两个排序的整数数组A和B变成一个新的数组.给出A=[1,2,3,4],B=[2,4,5,6],返回 [1,2,2,3,4,4,5,6] 假设A具有足够的空间(A数组的大小大于或等于m+n)去添加B ...
- queue,stack的相互实现
Implement Queue using Stacks [抄题]: [思维问题]: [一句话思路]: 取头部.取出来的时候,用一个output来倒序 [输入量]:空: 正常情况:特大:特小:程序里处 ...
- 22-maven-安装与配置
转载:https://blog.csdn.net/wy727764020/article/details/80595451 Maven的安装以及eclipse中配置maven 2018年06月06日 ...
- .zip/.rar打包与解压
Linux下如何解压.zip和.rar文件,对于Window下的常见压缩文件.zip和.rar,Linux也有相应的方法来解压它们: 1)对于zip linux下提供了zip和unzip程序,zip是 ...
- DB2与oracle类型对比
本文摘自http://www.cnblogs.com/cy163/archive/2010/11/17/1880280.html 做过DB2数据库应用迁移的工程师,了解IBM MTK工具在迁移过程中所 ...
- inline 引起undefined reference to
main.cc:57: undefined reference to `evpp::udp::UdpDecoder::GetHeader()'collect2: error: ld returned ...
- Debian 采用 iso 镜像作为 apt 源
1.将N个debian-506-amd64-DVD-N.iso存放于本地或其他媒介内,本例是放在本机/iso/目录下2.创建N个挂载点目录 如下: debian:~#mkdir –r /media/d ...
- eclipse 远程调试mapreduce
使用环境:centos6.5+eclipse(4.4.2)+hadoop2.7.0 1.下载eclipse hadoop 插件 hadoop-eclipse-plugin-2.7.0.jar 粘贴到 ...