2

记算法面试题--爬楼梯

 1 year ago
source link: https://www.lpime.cn/article/127
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

记算法面试题--爬楼梯

发布于 2022-10-14 / C++ / 没有评论 / 6浏览

爬楼梯C++

你在爬楼梯,需要n步才能爬到楼梯顶部,每次你只能向上爬1步或者2步。有多少种方法可以爬到楼梯顶部?

class Solution {
public:
    /**
     * 
     * @param n int整型 
     * @return int整型
     */
    int climbStairs(int n) {
        // write code here
        if(n == 1) 
            return 1;
        std::vector<int> dp(n+1);
        dp[1] = 1;
        dp[2] = 2;
        for(int i=3; i<=n ; i++)
        {
            dp[i] = dp[i-1] + dp[i-2];
        }
        return dp[n];
    }
};

本文由 Ryan 创作,采用 知识共享署名4.0 国际许可协议进行许可
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名
最后编辑时间为: 2022/10/14 17:04


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK