[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: There are two ways to climb to the top.
1. 1 step + 1 step
2. 2 steps

Example 2:

Input: 3
Output: 3
Explanation: There are three ways to climb to the top.
1. 1 step + 1 step + 1 step
2. 1 step + 2 steps
3. 2 steps + 1 step

解法:动态规划DP(Dynamic Programming)入门题。

state: dp[i] 表示爬到第i个楼梯的所有方法的和
function: dp[i] = dp[i-1] + dp[i-2]  //因为每次走一步或者两步, 所以dp[i]的方法就是它一步前和两步前方法加和
initial: dp[0] = 0; dp[1] = 1
end : return dp[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];
    }
}

Java:

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

Python: DP

class Solution(object):
    def climbStairs(self, n):
        if n < 3:
            return n
        dp = [0] * n
        dp[0] = 1
        dp[1] = 2
        for i in range(2, n):
            dp[i] = dp[i-2] + dp[i-1]

        return dp[n-1]  

Python:  DP, Time: O(n) Space: O(1)

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

Python:  Recursion,Time:  O(2^n) Space: O(n)

class Solution:
    def climbStairs1(self, n):
        if n == 1:
            return 1
        if n == 2:
            return 2
        return self.climbStairs(n - 1) + self.climbStairs(n - 2)

C++:

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

 

类似题目:

[LeetCode] 53. Maximum Subarray 最大子数组

[LeetCode] 746. Min Cost Climbing Stairs

[Airbnb] Max Sum of Non-consecutive Array Elements

 

All LeetCode Questions List 题目汇总

  

  

  

 

 

 

 

 

 

posted @ 2018-02-27 05:52  轻风舞动  阅读(1467)  评论(0编辑  收藏  举报