Leetcode 70. Climbing Stairs 爬楼梯 (递归,记忆化,动态规划)
2018-02-06 13:50
495 查看
题目描述
要爬N阶楼梯,每次你可以走一阶或者两阶,问到N阶有多少种走法测试样例
Input: 2 Output: 2 Explanation: 到第二阶有2种走法 1. 1 步 + 1 步 2. 2 步 Input: 3 Output: 3 Explanation: 到第三阶有3种走法 1. 1 步 + 1 步 + 1 步 2. 1 步 + 2 步 3. 2 步 + 1 步
详细分析
算法实现
记忆化递归(√)class Solution { public: int climbStairs(int n) { this->n = n; memo = new int[n+2]; for(int i=0;i<n+2;i++){ memo[i] = -1; } return recursiveClimbing(0); } int recursiveClimbing(int currentStep){ if(memo[currentStep]!=-1){ return memo[currentStep]; } if(currentStep==n){ return 1; } if(currentStep>n){ return 0; } memo[currentStep] = recursiveClimbing(currentStep+1) + recursiveClimbing(currentStep+2); return memo[currentStep]; } private: int n; int total = 0; int *memo; };
相关文章推荐
- Leetcode 70. Climbing Stairs 爬楼梯 (递归,记忆化,动态规划)
- LeetCode 70 Climbing Stairs(爬楼梯)(动态规划)(*)
- Leetcode 70 Climbing Stairs 爬楼梯的方法(动态规划)
- 分别用递归和动态规划实现爬楼梯的算法
- 【poj1579】Function Run Fun (动态规划DP递归,记忆化递归)
- LeetCode 198. House Robber 递归->记忆->动态规划 ->新思路
- Leetcode 746. Min Cost Climbing Stairs 最小成本爬楼梯 (动态规划)
- Leetcode 746. Min Cost Climbing Stairs 最小成本爬楼梯 (动态规划)
- LeetCode 70 Climbing Stairs(爬楼梯)(动态规划)(*)
- [leetcode]爬楼梯的递归和非递归方法
- 一道题看懂递归、(深度搜索)dfs、记忆化搜索、动态规划(DP)的差别!
- leetcode-爬楼梯(动态规划)
- lintcode:爬楼梯(递归,迭代,动态规划)
- LeetCode Climbing Stairs 递归求解和动态规划法
- leetcode_120题——Triangle (动态规划)
- 【LeetCode-动态规划】Minimum Path Sum【重要->状态转移方程取最值】
- 【动态规划】【记忆化搜索】【搜索】CODEVS 1262 不要把球传我 2012年CCC加拿大高中生信息学奥赛
- 贪心,递归,动态规划,及分治算法之间的区别和联系(四)
- java算法的基本思想之迭代,递归,动态规划
- leetcode 77. Combinations-排列|递归|非递归|Java|Python