题解 | #机器人的运动范围#

机器人的运动范围

http://www.nowcoder.com/practice/6e5207314b5241fb83f2329e89fdecc8

public class Solution {
    public int movingCount(int threshold, int rows, int cols) {
        boolean[][] v = new boolean[rows + 1][cols + 1];
        return helper(threshold, 0, 0, v, rows, cols);
    }

    public int helper(int threshold, int i, int j, boolean[][] v, int rows, int cols){
        if(i < 0 || i >= rows || j < 0 || j >= cols || v[i][j] || numSum(i) + numSum(j) > threshold) {
            return 0;
        }
        v[i][j] = true;
        return helper(threshold, i + 1, j, v, rows, cols)
            + helper(threshold, i - 1, j, v, rows, cols)
            + helper(threshold, i, j + 1, v, rows, cols)
            + helper(threshold, i, j - 1, v, rows, cols)
            + 1;
    }

    public int numSum(int num){
        int sum = 0;
        while(num != 0){
            int k = num%10;
            sum += k;
            num /= 10;
        }
        return sum;
    }
}
全部评论

相关推荐

牛客101244697号:这个衣服和发型不去投偶像练习生?
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务