刷题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);
        }
};

非常不好意思,Time Limit Exceeded

那就用dp算法吧:

class Solution{
    public:
        int climbStairs(int n){
            if(n==1) return 1;
            if(n==2) return 2;
            vector<int>dp;
            dp.push_back(1);
            dp.push_back(2);
            for(int i=2;i<n;i++){
                dp.push_back(dp[i-1]+dp[i-2]);
            }
            return dp[n-1];
        }
};

性能:

Runtime: 4 ms, faster than 55.03% of C++ online submissions for Climbing Stairs.
Memory Usage: 8.4 MB, less than 51.47% of C++ online submissions for Climbing Stairs.

三、优化措施

不优化了!

原文地址:https://www.cnblogs.com/siweihz/p/12249161.html

时间: 2024-11-07 12:42:07

刷题70. Climbing Stairs的相关文章

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)

377. Combination Sum IV 70. Climbing Stairs

back function (return number) remember the structure class Solution { int res = 0; //List<List<Integer>> resList = new ArrayList<List<Integer>>(); public int combinationSum4(int[] nums, int target) { Arrays.sort(nums); return back(

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 Add to List

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? Subscribe to see which companies asked this question ///////////233333333一看题有点动态规划和组

LeetCode 70 Climbing Stairs(爬楼梯)(动态规划)(*)

翻译 你正在爬一个楼梯. 它须要n步才干究竟顶部. 每次你能够爬1步或者2两步. 那么你有多少种不同的方法爬到顶部呢? 原文 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? 分析 动态规划基础题,首先设置3个变量用于

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

70. Climbing Stairs

1. 问题描述 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?Tags:Dynamic Programming 2. 解题思路 简单分析之: f(1) = 1; f(2) = 2; f(3) = f(2) + f(1)

[LeetCode] NO. 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个台阶的前一步,有两种情况:一种是在第n-1个台阶,爬1个台阶到达:另外一种是在第n-2个台阶上

[leedcode 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? public class Solution { public int climbStairs(int n) { //f(n)=f(n-1)+f(n-2);DP int