leetcode 题号1030 Matrix Cells in Distance Order

本文介绍了LeetCode第1030题,要求根据曼哈顿距离对矩阵中的单元格进行排序。提供了解题思路和代码实现,其中解题思路是利用散列值和链表法解决距离相同的情况,并按散列值顺序输出。
摘要由CSDN通过智能技术生成

查看题目详情可点击此处

题目

We are given a matrix with R rows and C columns has cells with integer coordinates (r, c), where 0 <= r < R and 0 <= c < C.

Additionally, we are given a cell in that matrix with coordinates (r0, c0).

Return the coordinates of all cells in the matrix, sorted by their distance from (r0, c0) from smallest distance to largest distance. Here, the distance between two cells (r1, c1) and (r2, c2) is the Manhattan distance, |r1 - r2| + |c1 - c2|. (You may return the answer in any order that satisfies this condition.)

Example 1:

Input: R = 1, C = 2, r0 = 0, c0 = 0
Output: [[0,0],[0,1]]
Explanation: The distances from (r0, c0) to other cells are: [0,1]

Example 2:

Input: R = 2, C = 2, r0 = 0, c0 = 1
Output: [[0,1],[0,0],[1,1],[1,0]]
Explanation: The distances from (r0, c0) to other cells are: [0,1,1,2]
The answer [[0,1],[1,1],[0,0],[1,0]] would also be accepted as correct.

Example 3:

Input: R = 2, C = 3, r0 = 1, c0 = 2
Output: [[1,2],[0,2],[1,1],[0,1],[1,0],[0,0]]
Explanation: The distances from (r0, c0) to other cells are: [0,1,1,2,2,3]
There are other answers that would also be accepted as correct, such as [[1,2],[1,1],[0,2],[1,0],[0,1],[0,0]].

Note:

1 <= R <= 100
1 <= C <= 100
0 <= r0 < R
0 <= c0 < C

解题思路

题目希望将矩阵中的点按与矩阵中某个点(以参数传入方式确认)的距离进行从小到大排序输出,两个点的距离如何计算不需要担心,题目中明确指出可以通过 |r1 - r2| + |c1 - c2| 计算得到。其实简单点解决,我们只需要将经典的排序算法,如快排的比较方式修改一下就可以解决问题。但我使用另外一种方式解决,因为点之间的距离会出现相同的情况,所以我将点之间的距离作为散列值,此处的散列冲突以链表法解决,遍历完矩阵点后,将散列表按散列值先后顺序拼接为数组输出即可。

代码实现

代码实现中主要关注 orderMatrixByDistance 方法。

public int getDistance(int[] pointA, int[] pointB) {
    return getPointAbs(0, pointA, pointB) + getPointAbs(1, pointA, pointB);
}

private int getPointAbs(int i, int[] pointA, int[] pointB) {
    return Math.abs(pointA[i] - pointB[i]);
}

public int[][] orderMatrixByDistance(int row, int col, int x, int y) {
    int[] basePoint = new int[]{x, y};
    LinkedList<Point>[] distances = new LinkedList[row * col];
    for (int i = 0; i < distances.length; i++) {
        distances[i] = new LinkedList();
    }

    for (int i = 0; i < row; i++) {
        for (int j = 0; j < col; j++) {
            int distance = getDistance(new int[]{i, j}, basePoint);
            distances[distance].add(new Point(i, j));
        }
    }

    Point[] sortedPoint = transLinkedToArray(row * col, distances);
    int[][] sortedIntPoint = new int[row * col][2];
    for (int i = 0; i < sortedPoint.length; i++) {
        sortedIntPoint[i] = sortedPoint[i].transInt();
    }

    return sortedIntPoint;
}

private Point[] transLinkedToArray(int size, LinkedList<Point>[] distances) {
    Point[] sortedDistance = new Point[size];
    int count = 0;
    for (int i = 0; i < distances.length; i++) {
        LinkedList<Point> distanceLinked = distances[i];
        if (distanceLinked.isEmpty()) {
            continue;
        }
        Point[] distanceArray = new Point[distanceLinked.size()];
        distanceLinked.toArray(distanceArray);
        System.arraycopy(distanceArray, 0, sortedDistance, count, distanceLinked.size());
        count += distanceLinked.size();
    }
    return sortedDistance;
}

private class Point {
    private final int x;
    private final int y;

    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public int[] transInt() {
        return new int[]{x, y};
    }
}

代码详情可点击查看 我的 GitHub 仓库

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值