[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 ...
随机推荐
- Python 列表表达式 ,迭代器(2) Yield
.yield 暂存为list def max_generator(numbers): current_max = for i in numbers: current_max = max(i, curr ...
- cmd 字符串截取
@echo off set "url=www.mzwu.com" echo 1.字符串截取 echo %url:~4,4% echo %url:~4,-4% echo %url:~ ...
- JPA调用函数
criteriaBuilder.function("udf_get_cc_userright", Integer.class, criteriaBuilder.literal(se ...
- Matlab中插值函数汇总(上)
Matlab中插值函数汇总分上下两个部分,主要整合自matlabsky论坛dynamic发表于2009-2-21 21:53:26 的主题帖,以及豆丁网rickoon上传的教材第8章<插值,拟合 ...
- cin中函数的作用
cin是istream类的对象,它是从标准输入设备(键盘)获取数据,程序中的变量通过流提取符">>"从流中提取数据.流提取符">>"从流 ...
- 1.Two Sum (Array; HashTable)
Given an array of integers, find two numbers such that they add up to a specific target number. The ...
- NetworkStateReceiver的简单应用
一.网络状态接收者是一个广播接收者当网络状态发生改变时会收到网络状态改变的广播 本例当网络状态改变时会进行相应处理 例如当断网时会发出通知点击通知后 会打开网络设置界面 代码如下: package c ...
- ROS学习笔记三(理解ROS节点)
要求已经在Linux系统中安装一个学习用的ros软件包例子: sudo apt-get install ros-indigo-ros-tutorials ROS图形概念概述 nodes:节点,一个节点 ...
- 淘宝、天猫又开源了一个动态化、高性能的UI框架
前言 淘宝.天猫一直致力于解决 页面动态化的问题 在2017年的4月发布了v1.0解决方案:Tangram模型 及其对应的 Android库 vlayout,该解决方案在手机淘宝.天猫 Android ...
- mysql添加注释
-- 查看字段类型-- show columns from campaign_distribute --给表添加注释 -- alter table campaign_distribute commen ...