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个变量用于转换:

int dp1 = 1, dp2 = 2, dpWay = 0;

依据题意,一次仅仅能是一步或两步。所以当n等于2时,有两种走法:1+1,2。

if (n <= 1) return dp1;
if (n == 2) return dp2;

从3開始,由于能够直接获得它的步数结果。所以直接写成:

while ((n--)-2) {
}

终于里面的变化方式为:

dpWay = dp1 + dp2;
dp1 = dp2;
dp2 = dpWay;

上一篇博客:LeetCode 206 Reverse Linked List(反转链表)(四步将递归改写成迭代)(*) ,介绍了怎样将递归改写成迭代,看过的童鞋应该会认为很easy的。那么这里再来转换一次:

int climbStairsIter(int n,  int dpWay,int dp1, int dp2) {
    if (n <= 1) return dp1;
    if (n == 2) return dp2;
    if ((n--) - 2) {
        dpWay = dp1 + dp2;
        dp1 = dp2;
        dp2 = dpWay;
        return climbStairsIter(n, dpWay, dp1, dp2);
    }
    else return dpWay;
}

int climbStairs(int n) {
    return climbStairsIter(n, 0,1,2);
}

由于这里的參数涉及到运行前面顺序,所以不妨单独列出来了,只是这样看来略不简洁呐。

代码

class Solution {
public:
    int climbStairs(int n) {

        int dp1 = 1, dp2 = 2, dpWay = 0;
        if (n <= 1) return dp1;
        if (n == 2) return dp2;

        while ((n--) - 2) {
            dpWay = dp1 + dp2;
            dp1 = dp2;
            dp2 = dpWay;
        }
        return dpWay;
    }
};
时间: 2024-10-10 06:24:24

LeetCode 70 Climbing Stairs(爬楼梯)(动态规划)(*)的相关文章

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

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

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 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? Note: Given n will be a positive integer. Example 1: Input: 2 Output: 2 Explanation:

[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 ----- 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? 还是简单的动态规划问题,很明显可以发现. dp[i] = dp[i-2]+dp[i-1] public class Solution { public int clim