码迷,mamicode.com
首页 > 其他好文 > 详细

[CareerCup] 9.1 Climbing Staircase 爬楼梯

时间:2015-09-17 06:27:41      阅读:142      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!