LN : leetcode 70 Climbing Stairs

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

Note: Given n will be a positive integer.

DP Accepted

这是一道特别经典又简单的动态规划题目,先考虑简单的情况,上第一层的方式只有1种,而上第二层的方式有两种。再考虑复杂的情况,上第n层的前一层要么是n-1层,要么是n-2层。其实,该问题很类似斐波那契数列,还是很容易解答的。

class Solution {
public:
    int climbStairs(int n) {
        vector<int> stairs(n+1, 0);
        stairs[1] = 1;
        stairs[2] = 2;
        for (int i = 3; i < n+1; i++)   stairs[i] = stairs[i-1]+stairs[i-2];
        return stairs[n];
    }
};
原文地址:https://www.cnblogs.com/renleimlj/p/7746346.html