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.

测试代码(c++):

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

性能:

这里写图片描述

猜你喜欢

转载自blog.csdn.net/m0_37625947/article/details/77600693