LeetCode Climbing Stairs 爬楼梯

递归法(TLE代码):

 1 class Solution {
 2 public:
 3     int climbStairs(int n) {
 4         if(n==0)
 5             return 1;
 6         if(n<0)
 7             return 0;
 8         return (climbStairs(n-1)+climbStairs(n-2));
 9     }
10 };

动态规划法:

 1 class Solution {
 2 public:
 3     int climbStairs(int n) {
 4         if(n==1) return 1;
 5         if(n==2) return 2;
 6         int a=1,b=2,c=0,i;
 7         for( i=0;i<n-2 ;i++ ){
 8             c=a+b;
 9             a=b;
10             b=c;
11         }
12         return c;
13     }
14 };

题意:爬楼梯,给出一条楼梯的阶数,每次可以走1步,或者两步一起走,那么就有两种走法走完这n阶。问有多少种走法走完这楼梯,返回它。

思路:当n=1时,返回1;当n=2时,返回2。当n>3时,要返回的是n-1和n-2所要返回的数。比如n=3,那么就返回1+2的值,n=4,返回3+2。类推下去。

吐槽:原来很困惑这个问题,用递归写了一下,发现不行,虽然代码很简单。之后考虑各种方法,再用此递归法计算一下各个返回值,发现了规律,原来如此。~

时间: 2024-08-09 10:39:15

LeetCode Climbing Stairs 爬楼梯的相关文章

LeetCode | 0070. Climbing Stairs爬楼梯【Python】

LeetCode 0070. Climbing Stairs爬楼梯[Easy][Python][动态规划] Problem LeetCode 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

Climbing Stairs爬楼梯——动态规划

题目描写叙述: 初阶:有n层的台阶,一開始你站在第0层,每次能够爬两层或者一层. 请问爬到第n层有多少种不同的方法? 进阶:假设每次能够爬两层.和倒退一层,同一个位置不能反复走,请问爬到第n层有多少种不同的方法? 解题思路: 初阶:一维动态规划.爬楼梯数目事实上是一个斐波拉契数列. 假定f[i] 表示是爬到第i层的方法,那么f[i] = f[i-1] + f[i-2] //第i层的方法数目等于第i-1层数目加上第i-2层数目. 初值:f[0] = 1, f[1] = 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 1 or 2 steps. In how many distinct ways can you climb to the top? 解题思路: 动态规划DP(Dynamic Programming)入门题. state: dp[i] 表示爬到第i个楼梯的所有方法的和function: dp[i] =

[LeetCode] Min Cost Climbing Stairs 爬楼梯的最小损失

On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once you pay the cost, you can either climb one or two steps. You need to find minimum cost to reach the top of the floor, and you can either start from the step w

Climbing Stairs 爬楼梯问题,每次可以走1或2步,爬上n层楼梯总方法 (变相fibnacci)

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? 当n=1时,有1种方法,即直接走1步 当n=2时,有2方法:连续走2步,或直接走两步 对于n,设f(n)为总方法,则 f(n) = f(n-1)+f(n-2) ps:f

[leetcode]Climbing Stairs @ Python

原题地址:https://oj.leetcode.com/problems/climbing-stairs/ 题意: 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? 解题思路: 爬楼梯问题.经典的动态规划问题.每次上

[LeetCode] Climbing Stairs [24]

题目 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? 原题链接(点我) 解题思路 爬楼梯:一次可以爬1阶或者2阶,问爬n阶楼梯有多少方法? 这是个典型的斐波拉切应用场景,我们下面来分析下: 对于1阶,只有 1 种方法

[CareerCup] 9.1 Climbing Staircase 爬楼梯

9.1 A child is running up a staircase with n steps, and can hop either 1 step, 2 steps, or 3 steps at a time. Implement a method to count how many possible ways the child can run up the stairs. LeetCode上的原题,请参见我之前的博客Climbing Stairs 爬梯子问题. class Solut

LeetCode: Climbing Stairs [070]

[题目] 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? [题意] 有个梯子有n阶,每次只能爬1阶或者2阶,为爬到梯子顶共有多少种爬法 [思路] 依次确定跳到每一阶上的爬法数目 这其实是一个斐波那契数列数列.假设A[