leetcode || 70、 Climbing Stairs

problem:

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?

Hide Tags

Dynamic Programming

题意:爬上一个 n阶 的楼梯,每次爬一阶或者两阶,求一共有多少不同的方法

thinking:

(1)说实话,看到题第一反应使用 DFS ,但是深搜的时间复杂度为解个数的线性表达式,肯定会超时

(2)提示使用DP,时间复杂度为O(n),这道题的状态转移方程式很简单: a[i]=a[i-1]+a[i-2];唯一有问题的是初始化:a[0]=1,a[1]=2

提交试错时改正就是。

code:

class Solution {
public:
    int climbStairs(int n) {
        vector<int> a(n,0);
        a[0]=1;
        a[1]=2;
        if(n<3)
            return a[n-1];
        for(int i=2;i<n;i++)
            a[i]=a[i-1]+a[i-2];
        return a[n-1];    

    }
};
时间: 2024-10-08 21:33:13

leetcode || 70、 Climbing Stairs的相关文章

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阶楼梯,每次只能爬1阶或2阶楼梯,问爬到第n阶楼梯共有几种爬法-_-||.题目可以看成是,设f(n)表示爬到第n 阶楼梯的方法数,为

【LeetCode】070. 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? Note: Given n will be a positive integer. 题解: 爬到第n层的方法要么是从第n-1层一步上来的,要不就是从n-2层2步

LeetCode题解之Climbing Stairs

1.题目描述 2.问题分析 使用动态规划. 3.代码 1 int climbStairs(int n) { 2 if( n <= 2){ 3 return n; 4 } 5 6 int dp[n+1]; 7 dp[0] = 0; 8 dp[1] = 1; 9 dp[2] = 2; 10 for( int i = 3; i <= n ; i++){ 11 dp[i] = dp[i-1] + dp[i-2]; 12 } 13 14 return dp[n]; 15 } 原文地址:https://w

[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

[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

leetCode 70. Climbing Stairs | 动态规划

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? 思考: top steps 1 1 2 2 3 3 4 5 5 8 6 13 ... ... 从上面的分析可以看出,f(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

一.题目说明 题目70. Climbing Stairs,爬台阶(楼梯),一次可以爬1.2个台阶,n层的台阶有几种爬法.难度是Easy! 二.我的解答 类似的题目做过,问题就变得非常简单.首先用递归方法计算: class Solution{ public: int climbStairs(int n){ if(n==1) return 1; if(n==2) return 2; return climbStairs(n-1) + climbStairs(n-2); } }; 非常不好意思,Tim

[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? 这题比较简单,可以使用动态规划来求解