[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] = dp[i-1] + dp[i-2]  //因为每次走一步或者两步, 所以f[i]的方法就是它一步前和两步前方法加和
initial: dp[0] = 0; dp[1] = 1
end : return f[n]

Java: Method 1: Time: O(n), Space: O(n)

public int climbStairs(int n) {
    int[] dp = new int[n + 1];
    dp[0] = 1;
    dp[1] = 1;
    for (int i = 2; i <= n; i++) {
        dp[i] = dp[i - 1] + dp[i - 2];
    }
    return dp[n];
}

Java: Method 2: Time:  O(n), Space: O(1) 

public int climbStairs(int n) {
    if (n == 0 || n == 1 || n == 2){
        return n;
    }
    int [] dp = new int[3];
    dp[1] = 1;
    dp[2] = 2;
    for (int i =3; i <= n; i++) {
        dp[i%3] = dp[(i-1)%3] + dp[(i-2)%3];
    }
    return dp[n%3];
}

Java: Method 3: Time:  O(n), Space: O(1)

public class Solution {
    public int climbStairs(int n) {
        int[] dp = new int[]{0,1,2};
        if(n < 3) return dp[n];
        for(int i = 2; i < n; i++){
            dp[0] = dp[1];
            dp[1] = dp[2];
            dp[2] = dp[0] + dp[1];
        }
        return dp[2];
    }
}

Python:

class Solution:
    # DP   Time: O(n) Space: O(1)
    def climbStairs(self, n):
        prev, current = 0, 1
        for i in xrange(n):
            prev, current = current, prev + current,
        return current

    # Recursion   Time:  O(2^n) Space: O(n)
    def climbStairs1(self, n):
        if n == 1:
            return 1
        if n == 2:
            return 2
        return self.climbStairs(n - 1) + self.climbStairs(n - 2)

  

  

  

 

原文地址:https://www.cnblogs.com/lightwindy/p/8476881.html

时间: 2024-10-11 23:06:54

[LeetCode] 70. Climbing Stairs 爬楼梯的相关文章

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

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

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(爬楼梯)(动态规划)(*)

翻译 你正在爬一个楼梯. 它须要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] 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

[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. 1.递归(超时) public int climbStairs(int n) { i