题目:

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?

Note: Given n will be a positive integer.

Example 1:

Input: 2
Output: 2
Explanation: There are two ways to climb to the top.
1. 1 step + 1 step
2. 2 steps

Example 2:

Input: 3
Output: 3
Explanation: There are three ways to climb to the top.
1. 1 step + 1 step + 1 step
2. 1 step + 2 steps
3. 2 steps + 1 step

分析:

每次可以爬1阶或2阶,求爬n阶楼梯有多少种爬法。

我们知道要爬到第n阶,可以由第n-1阶爬1阶和第n-2阶爬2阶完成,所以f(n) = f(n-1) + f(n-2)。但使用递归会超时。

我们可以开辟一个大小为n+1的数组nums,nums[i]表示爬到第i阶有多少中爬法,依据前面的公式可以求出。

此外我们可以定义f,s,res三个变量来代替数组,因为实际上在求第i阶有多少种爬法时,只与i-1和i-2有关,所以我们可以用f表示前一个楼梯爬的个数,s表示后一个楼梯爬的个数,res表示当前求的,没求一次,更新一下f,s的值即可。

程序:

class Solution {
public:
int climbStairs(int n) {
if(n == ) return ;
if(n == ) return ;
int f = , s = , res = ;
for(int i = ; i <= n; ++i){
res = f + s;
f = s;
s = res;
}
return res;
}
};
// class Solution {
// public:
// int climbStairs(int n) {
// vector<int> nums(n+1);
// nums[0] = 1;
// nums[1] = 1;
// for(int i = 2; i <= n; ++i)
// nums[i] = nums[i-1] + nums[i-2];
// return nums[n];
// }
// };

LeetCode 70. Climbing Stairs爬楼梯 (C++)的更多相关文章

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

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

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

  4. Leetcode 70. Climbing Stairs 爬楼梯 (递归,记忆化,动态规划)

    题目描述 要爬N阶楼梯,每次你可以走一阶或者两阶,问到N阶有多少种走法 测试样例 Input: 2 Output: 2 Explanation: 到第二阶有2种走法 1. 1 步 + 1 步 2. 2 ...

  5. 70. Climbing Stairs爬楼梯

    网址:https://leetcode.com/problems/climbing-stairs/ 其实就是斐波那契数列,没什么好说的. 注意使用3个变量,而不是数组,可以节约空间. class So ...

  6. Leetcode#70. Climbing Stairs(爬楼梯)

    题目描述 假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数. 示例 1: 输入: 2 输出: 2 解 ...

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

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

  9. leetCode 70.Climbing Stairs (爬楼梯) 解题思路和方法

    Climbing Stairs  You are climbing a stair case. It takes n steps to reach to the top. Each time you ...

随机推荐

  1. python中判断实例可迭代地几种方式

    1. 利用 __iter__内建属性 if hasattr(obj, '__iter__') : print 'iterable' 这种方法不能检测字符串,如:hasattr('', '__iter_ ...

  2. 2019 wannafly winter camp

    2019 wannafly winter camp Name Rank Solved A B C D E F G H I J K day1 9 5/11 O O O O O day2 5 3/11 O ...

  3. Universal-Image-Loader源码分析(二)——载入图片的过程分析

    之前的文章,在上面建立完config之后,UIl通过ImageLoader.getInstance().init(config.build());来初始化ImageLoader对象,之后就可以用Ima ...

  4. kubernetes备份和恢复

    kubernetes备份和恢复   备份etcd数据 首先由于ETCD有三个备份,并且会同步,所以您只需要在一台master机器上执行ETCD备份即可. 另外在运行下列命令前,确保当前机器的kube- ...

  5. Failed to fetch URL https://dl-ssl.google.com/android/repository/addons_list-2.xml

    解决方法来源:http://www.cnblogs.com/kaka-bing/archive/2012/10/31/2747490.html 问题描述: 使用Android SDK Manager检 ...

  6. 备份时如何排除掉默认的 information_schema 和 mysql 库?

    备份时如何排除掉默认的 information_schema 和 mysql 库? mysql -e "show databases;" -uroot -ppassword | g ...

  7. 1-关于单片机通信数据传输(中断发送,大小端,IEEE754浮点型格式,共用体,空闲中断,环形队列)

    补充: 程序优化 为避免普通发送和中断发送造成冲突(造成死机,复位重启),printf修改为中断发送 写这篇文章的目的呢,如题目所言,我承认自己是一个程序猿.....应该说很多很多学单片机的对于... ...

  8. 串口通信DMA中断

    这是以前学32的时候写的,那时候学了32之后感觉32真是太强大了,比51强的没影.关于dma网上有许多的资料,亲们搜搜,这里只贴代码了,其实我也想详详细细地叙述一番,但是自己本身打字就慢,还有好多事情 ...

  9. c# 多线程和异步

    有时候需要多线程来运行处理后端和UI的更新. 通常用 ((Action)delegate() { if (isBussy) { return; } isBussy = true; while (tru ...

  10. 20155337《网络对抗》Exp5 MSF基础应用

    20155337<网络对抗>Exp5 MSF基础应用 实践目标 本实践目标是掌握metasploit的基本应用方式,重点常用的三种攻击方式的思路.具体需要完成: 1.1一个主动攻击实践,如 ...