首页 > 其他 > 详细

Leetcode789 阻碍逃脱者 曼哈顿距离

时间:2021-02-24 10:21:22      阅读:20      评论:0      收藏:0      [点我收藏+]

技术分享图片 

  JAVA:

    public final boolean escapeGhosts(int[][] ghosts, int[] target) {
        int shortest = shortest(new int[]{0, 0}, target);
        for (int[] ghost : ghosts) {
            if (shortest(target, ghost) <= shortest) return false;
        }
        return true;
    }

    private final int shortest(int[] source, int[] target) {
        return Math.abs(source[0] - target[0]) + Math.abs(source[1] - target[1]);
    }

  JS:

var escapeGhosts = function (ghosts, target) {
    let shortest = getShortest(target, [0, 0]);
    for (let i = 0; i < ghosts.length; i++) {
        if (getShortest(target, ghosts[i]) <= shortest) return false;
    }
    return true;
};

var getShortest = function (position0, position1) {
    return Math.abs(position0[0] - position1[0]) + Math.abs(position0[1] - position1[1]);
}

技术分享图片

 

Leetcode789 阻碍逃脱者 曼哈顿距离

原文:https://www.cnblogs.com/niuyourou/p/14439022.html

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