Leetcode_70_Climbing Stairs_ stairs

Topic Description: Suppose you are climbing stairs. N order you need to get to the roof.
Every time you can climb one or two steps. How many different ways can climb to the roof of it?
Note: Given n is a positive integer.

输入样例:
输入: 2
输出: 2
解释: 有两种方法可以爬到楼顶。
1.  1 阶 + 1 阶
2.  2 阶
输入样例:
输入: 3
输出: 3
解释: 有三种方法可以爬到楼顶。
1.  1 阶 + 1 阶 + 1 阶
2.  1 阶 + 2 阶
3.  2 阶 + 1 阶

A algorithm: Memory of recursion
in violence law, we will put all the order may be combined to climb, that is, 1 and 2. In each step we will continue to call this function analog climb climbStairs 1st and 2nd order situation, and returns the sum of two functions.
climbStairs (i, n-) = (i +. 1, n-) + climbStairs (i + 2, n-)
climbStairs (i, n-) = (i +. 1, n-) + climbStairs (i + 2, n-)
where i is defined the current order, and n defines the target order.
We can store the results of each step in the stair array, whenever the function is called again, we will return the results directly from the stair array.
With the help of stair array, we get a fix recursive tree, reducing its size to n.

class Solution {
public:
    int climb_stair(int i,int n,int *stair){
        if(i>n)
            return 0;
        if(i==n)
            return 1;
        if(stair[i]!=-1)
            return stair[i];
        stair[i]=climb_stair(i+1,n,stair)+climb_stair(i+2,n,stair);
        return stair[i];
    }
    int climbStairs(int n) {
        int stair[n];
        fill(stair,stair+n,-1);//初始化stair数组
        return climb_stair(0,n,stair);
    }
};

Here Insert Picture Description
Method two: Dynamic Programming
i-th can be obtained by the following two methods:
After the first (i-1) a step to climb order.
After the first (i-2) Order Order 2 to climb.
Therefore, the total number and the number of stage ii of the method is to reach into the first (i-1) and the step (i-2) order method.
Order dp [i] represents the number of ways to reach the first ii order:
dp [i] = DP [I-. 1] + DP [I-2]
dp [i] = DP [I-. 1] + DP [I-2 ]

int climbStairs(int n){
        if(n==1)//先进行判断,n是否是1阶
            return 1;
        int dp[n+1];
        memset(dp,0,sizeof(dp));
        dp[1]=1;
        dp[2]=2;
        if(n==1)
            return dp[1];
        if(n==2)
            return dp[2];
        for(int i=3;i<=n;i++){
            dp[i]=dp[i-1]+dp[i-2];
        }
        return dp[n];
    }

Here Insert Picture Description
Method 3: Use the Fibonacci columns
we use dpdp array, wherein dp [i] = dp [i -1] + dp [i-2]. It can be easily obtained by analyzing dp [i] is actually the first ii th Fibonacci number.
Fib (n) = Fib (n -1) + Fib (n-2)
Now we must find out as in 1 and 2 Fibonacci number in the n-th number of the first term and the second term, i.e. said Fib (1) = 1 and Fib (2) = 2

int climbStairs(int n){
        if(n==1)
            return 1;
        int one=1;
        int two=2;
        for(int i=3;i<=n;i++){
            int three=one+two;
            one=two;
            two=three;
        }
        return two;
    }

Here Insert Picture Description

Guess you like

Origin blog.csdn.net/all_about_WZY/article/details/90636974