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(n-1)即第一次走一步的走法,

f(n-2)即第一次走两步的走法

归回fibnacci问题解法:

 1 class Solution {
 2 public:
 3     int climbStairs(int n) {
 4         if(n < 0)
 5             return -1;
 6         int res[] = {0,1};
 7         if(n<2)
 8             return res[n];
 9
10         int fib1 = 0;
11         int fib2 = 1;
12
13         int result = 1;
14
15         for(int i = 1 ; i <= n ; i++){
16             result = fib1 + fib2;
17             fib1 = fib2;
18             fib2 = result;
19         }
20
21         return result;
22     }
23 };
时间: 2024-10-14 01:48:51

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

n阶楼梯,一次走1,2,3步,求多少种不同走法

##已知n阶楼梯,一次可以迈1,2,3步.求所有走法## 如果要列出走法,时间复杂度太高,O(n)=2**n,前两个函数遍历走法.## 如果只是单纯列出走法数量,就简单多了,也但是很容易内存爆表. ## n层走法,可以视为n-1层再走一步,n-2层走两步,n-3层走三步.题目都可以按这个思路解决import copy,timelv=5n1=1000000fzd=0lg=[]if lv<=1:    def dg(ln,n,l):        global fzd        fzd+=1  

70. Climbing Stairs【leetcode】递归,动态规划,java,算法

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. 题目分析:每次只能走1或2步,问n步的话有多少中走法???? 可以用动态规划和递归解

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 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

LeetCode 746. Min Cost Climbing Stairs (使用最小花费爬楼梯)

题目标签:Dynamic Programming 题目给了我们一组 cost,让我们用最小的cost 走完楼梯,可以从index 0 或者 index 1 出发. 因为每次可以选择走一步,还是走两步,这里用 dynamic, 从index 2 (第三格楼梯开始) 计算每一个楼梯,到达需要用的最小cost. 在每一个楼梯,只需要计算 从前面2格楼梯过来的cost, 和 从前面1格楼梯过来的 cost,哪个小.就选哪个叠加自己的cost.最后 index = len 的地方就是走到top 所用的最小

leetCode 70.Climbing Stairs (爬楼梯) 解题思路和方法

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? 思路:题目也比较简单,类似斐波那契. 代码如下: public class Solution { public int climbSta

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? 假设你在爬一个楼梯,该楼梯有n阶,你有两种爬法,每次爬一阶或者两阶.请问你有多少种爬法到达楼梯顶部. public int ClimbStairs(int n) { i

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? 这个爬梯子问题最开始看的时候没搞懂是让干啥的,后来看了别人的分析后,才知道实际上跟斐波那契数列非常相似,假设梯子有n层,那么如何爬到第n层呢,因为每次只能怕1或2步,那