首页 > 其他 > 详细

[8.1] Triple Step

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

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

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

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