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[i]表示爬到第i阶的爬法数

当i=1时,A[1]=1    (从梯子底只要爬一阶即可)

当i=2时,A[2]=2    (从第一阶往上再爬一阶,或者从梯子底直接爬谅解,所以是两种爬法)

当i>=3时,A[i]=A[i-1]+A[i-2]    (因为每次只能爬一阶或者两阶,因此要跳到当前阶,只可能从他的前一阶或者前前一阶往上爬)

【代码】

class Solution {
public:
    int climbStairs(int n) {
        if(n==0)return 0;
        if(n==1)return 1;
        if(n==2)return 2;

        vector<int>stairs(n, 0);
        stairs[0]=1;
        stairs[1]=2;
        for(int i=2; i<n; i++){
            stairs[i]=stairs[i-1]+stairs[i-2];
        }
        return stairs[n-1];
    }
};

LeetCode: Climbing Stairs [070]

时间: 2024-10-22 21:20:37

LeetCode: Climbing Stairs [070]的相关文章

[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 种方法

[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