[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 种方法, 记为f(1);

对于2阶,记为f(2):

最后一步只爬1阶,有 f(1);

最后一步一次爬2阶,即一步上来,这是1中方法;

综合下来 f(2) = f(1) +1 = 2;

对于3阶,记为f(3):

最有一步只爬1阶,有f(2)种方法

最后一步一次爬2阶,有f(1)种方法;

综合下来 f(3) = f(1) + f(2) = 3

对于4阶,记为f(4):

最有一步只爬1阶,有f(3)种方法

最后一步一次爬2阶,有f(2)种方法;

综合下来 f(4) = f(3) + f(2)

....

对于n阶,记为f(n):

最有一步只爬1阶,有f(n-1)种方法

最后一步一次爬2阶,有f(n-2)种方法;

综合下来 f(n) = f(n-1) + f(n-2).

想必通过以上分析,你应该能够看明白了,就是一个斐波拉切问题。

代码实现:

class Solution {
public:
    int climbStairs(int n) {
         if(n==0) return 0;
         if(n==1) return 1;
         if(n==2) return 2;
         int n1=1, n2 = 2;
         int sum = 0;
         for(int i=3;i<=n; ++i){
             sum = n1 + n2;
             n1 = n2;
             n2 = sum;
         }
         return sum;
    }
};

如果你觉得本篇对你有收获,请帮顶。

另外,我开通了微信公众号--分享技术之美,我会不定期的分享一些我学习的东西.

你可以搜索公众号:swalge 或者扫描下方二维码关注我

(转载文章请注明出处: http://blog.csdn.net/swagle/article/details/29562769
)

[LeetCode] Climbing Stairs [24]

时间: 2024-08-11 03:38:31

[LeetCode] Climbing Stairs [24]的相关文章

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

[LeetCode] Climbing Stairs (Sequence DP)

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

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? //第一种解法 class Solution { public: int climbStairs(int n) { int ans[100] = {1,2}; for

Leetcode: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层有f(n)中走法,因为每次只能走1步或2步,则 f(n) = f(n-1) + f(n-2) 很明显是一个斐波那契数,f(0) = 0,

[LeetCode] 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? Show Tags 这题其实就是斐波那契数列来的. #include <iostream> using namespace std; class Solution {

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][JavaScript]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? https://leetcode.com/problems/climbing-stairs/ 直接递归超时了,要动态规划. 打印前几个数

[LeetCode OJ]-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阶楼梯,每次只能爬1阶或2阶,问爬到顶端一共有多少种方法 方法一: 利用二叉树的思想,对于n=3时有3种方法,有几个叶子节点便有几种方法 1 void clim