Leetcode789 阻碍逃脱者 曼哈顿距离

 

  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]);
}

 

posted @ 2021-02-24 00:21  牛有肉  阅读(110)  评论(0编辑  收藏  举报