首页 > 其他 > 详细

[CareerCup] 9.1 Climbing Staircase 爬楼梯

时间:2015-09-17 06:24:56      阅读:187      评论:0      收藏:0      [点我收藏+]

 

9.1 A child is running up a staircase with n steps, and can hop either 1 step, 2 steps, or 3 steps at a time. Implement a method to count how many possible ways the child can run up the stairs.

 

LeetCode上的原题,请参见我之前的博客Climbing Stairs 爬梯子问题

 

class Solution {
public:
    int countWays(int n) {
        vector<int> res(n + 1, 1);
        for (int i = 2; i <= n; ++i) {
            res[i] = res[i - 1] + res[i - 2];
        }
        return res.back();
    }
};

 

[CareerCup] 9.1 Climbing Staircase 爬楼梯

原文:http://www.cnblogs.com/grandyang/p/4815094.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!