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

[8.1] Triple Step

时间:2016-12-08 07:40:17      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:case   man   and   can   style   amp   log   return   turn   

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.

 

    public static int getPossibleWays(final int n) {
        int[] memo = new int[n + 1];
        return helper(n, memo);
    }

    private static int helper(int i, int[] memo) {
        if(i == 0 || i == 1) return 1;
        if(i ==2) return 2;
        if(i >=3 && memo[i] == 0) {
            memo[i] = helper(i -1, memo) + helper(i -2, memo) + helper(i -3, memo);
        }
        return memo[i];
    }

 

[8.1] Triple Step

标签:case   man   and   can   style   amp   log   return   turn   

原文地址:http://www.cnblogs.com/Phoebe815/p/6143496.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!