题解 | #跳台阶#

跳台阶

https://www.nowcoder.com/practice/8c82a5b80378478f9484d87d1c5f12a4

class Solution {
public:
    int dp[50]{0}; // initialize an array to store the number of ways to jump to each floor
    int jumpFloor(int number) {
        dp[1] = 1; // there is only one way to jump to the first floor
        dp[2] = 2; // there are two ways to jump to the second floor
        for(int i=3; i<=number;i++){
            dp[i] = dp[i-1] + dp[i-2]; // the number of ways to jump to the i-th floor is the sum of the number of ways to jump to the (i-1)th floor and the (i-2)th floor
        }
        return dp[number]; // return the number of ways to jump to the n-th floor
    }
};

全部评论

相关推荐

缒梦&独舞:这家公司是这样的,去年给我实习offer了,不过也是面着玩儿的,他周六还要去做公益志愿活动
点赞 评论 收藏
分享
评论
2
2
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务