问题描述:
假设你正在爬楼梯。需要 n 阶你才能到达楼顶。
每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢?
注意:给定 n 是一个正整数。
示例 1:输入: 2
输出: 2
解释: 有两种方法可以爬到楼顶。
1. 1 阶 + 1 阶
2. 2 阶
示例 2:
输入: 3
输出: 3
解释: 有三种方法可以爬到楼顶。
1. 1 阶 + 1 阶 + 1 阶
2. 1 阶 + 2 阶
3. 2 阶 + 1 阶
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/climbing-stairs
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
执行结果:
代码描述:
class Solution {
public:
map<int, int> m;
int climbStairs(int n) {
if(m.count(n) > 0) return m[n];
int result = 0;
if(n < 4) return n;
else
{
result = climbStairs(n-1) + climbStairs(n-2);
}
m.insert(make_pair(n, result));
return result;
}
};
走台阶变形问题: 奇偶脚、循环实现等。
https://github.com/hitskyer/course/blob/master/dataAlgorithm/dongyaxing/Recursion/goStep.cpp