LeetCode 70. Climbing Stairs爬楼梯 (C++)
题目:
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++)的更多相关文章
- [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爬楼梯
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阶楼梯,每次你可以走一阶或者两阶,问到N阶有多少种走法 测试样例 Input: 2 Output: 2 Explanation: 到第二阶有2种走法 1. 1 步 + 1 步 2. 2 ...
- 70. Climbing Stairs爬楼梯
网址:https://leetcode.com/problems/climbing-stairs/ 其实就是斐波那契数列,没什么好说的. 注意使用3个变量,而不是数组,可以节约空间. class So ...
- Leetcode#70. Climbing Stairs(爬楼梯)
题目描述 假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数. 示例 1: 输入: 2 输出: 2 解 ...
- 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 ...
- 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 70.Climbing Stairs (爬楼梯) 解题思路和方法
Climbing Stairs You are climbing a stair case. It takes n steps to reach to the top. Each time you ...
随机推荐
- Rafy框架
l 什么是Rafy框架? -------- Rafy 是一个面向企业级开发的插件化快速开发框架. l Rafy的优点是什么? ------快速开发.产品线工程.一套代码可同时生成并运行 C/S.单 ...
- Hyper-v虚拟化
1.打开Hyper-V管理器,新建虚拟机 2.点击下一步,继续设置 3.设置虚拟机名称和存储位置,点击选择打钩,自定义路径 4.设置虚拟机运行内存 5.设置虚拟机设置好的网络 6.创建虚拟磁盘 7创建 ...
- mac系统默认python3.6
1. 终端打开.bash_profile文件 终端输入:open ~/.bash_profile 2. 打开.bash_profile文件后在内容最后添加 alias python=" ...
- SDN期末作业-通过SDN的应用实现负载均衡
负载均衡程序 1.程序链接:https://github.com/424baopu/software/tree/master/LoadBalance 2.场景 topo: 场景描述: 服务器host ...
- SDN 第三次上机作业
SDN 第三次上机作业 1.创建拓扑 2.利用OVS命令下发流表,实现vlan功能 3.利用OVS命令查看流表 s1: s2: 4.验证性测试 5.Wireshark 抓包验证
- .Net使用163smtp发送邮件时错误:邮箱不可用. has no permission解决方法
C#实现简单邮件发送代码如下 public static void SendAsync(string emailTo, string subject, string mailBody) { var m ...
- JavaScript中数组slice和splice的对比小结
前言 今天重温了一下Javascript,看到了数组的方法,其中有两个比较相似的方法——splice和splice,看着很像,就是多了一个p,但是用法却相当不一样. 在使用中,可以通过选择一个具有强语 ...
- Arcgis for Js之加载wms服务
概述:本节讲述Arcgis for Js加载ArcgisServer和GeoServer发布的wms服务. 1.定义resourceInfo var resourceInfo = { extent: ...
- BZOJ3173:[TJOI2013]最长上升子序列(Splay)
Description 给定一个序列,初始为空.现在我们将1到N的数字插入到序列中,每次将一个数字插入到一个特定的位置.每插入一个数字,我们都想知道此时最长上升子序列长度是多少? Input 第一行一 ...
- [国家集训队]Tree II
嘟嘟嘟 这道题其实还是挺基础的,只不过操作有点多. 区间乘和区间加按线段树的方式想. 那么就先要下放乘标记,再下放加标记.但这两个和反转标记是没有先后顺序的. 对于区间加,sum加的是区间长度\(*\ ...