题目:

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. Balanced Search Trees

    平衡搜索树 前面介绍的二叉搜索树在最坏情况下的性能还是很糟糕,而且我们不能控制操作的顺序,有时根本就不是随机的,我们希望找到有更好性能保证的算法. 2-3 search trees 于是先来了解下 2 ...

  2. 单一事件中心管理组件通信( vuex )

    有时候两个组件也需要通信(非父子关系).在简单的场景下,可以使用一个空的 Vue 实例作为中央事件总线: 补充$emit ,$on的讲解 代码: <!DOCTYPE html> <h ...

  3. Python2.7-robotparser

    robotparser 模块,用于解析网站的 robots.txt 文件,robots.txt 文件是用于指定搜索引擎爬虫的访问权限的,此模块在 python3 中重命名为 urllib.robotp ...

  4. CentOS7.5服务器安装(并添加用户) anaconda3 并配置 PyTorch1.0

    ===========================================================================================[admin@lo ...

  5. ASP.NET Response 下载文件

    private void DownLoad(string fileName, string path) { FileInfo fi = new FileInfo(path); if (fi.Exist ...

  6. Ubuntu 重新安装声卡驱动

    有的时候ubuntu 的声卡不能用,没有声音也不能使用麦克风,所有很困惑,查看声卡驱动的时候不显示声卡的驱动,所有我们要自己安装声卡驱动, 1.下载驱动包这是比较新的声卡驱动,1.0.20 $ wge ...

  7. 配置openfire环境

    Openfire 的安装和配置 1. 下载最新的openfire安装文件 官方下载站点:http://www.igniterealtime.org/downloads/index.jsp#openfi ...

  8. GitHub上最火的40个Android开源项目(二)

    21.drag-sort-listview DragSortListView(DSLV)是Android ListView的一个扩展,支持拖拽排序和左右滑动删除功能.重写了TouchIntercept ...

  9. day32

    今日内容 1.基于TCP协议(通信循环) 2.基于TCP协议(连接循环) 3.粘包问题 4.模拟SSH实现远程执行命令 服务器端 ################################### ...

  10. 关于Android开发环境的演变

    是不是我天生就不适合安装软件——经过eclipse.jdk.Android Studio的历次安装,我发觉自己似乎永远都装不好.去年eclipse断断续续装了三四天,那时希望能附加C++的软件包,却始 ...