在2d有序数组中查找行元素的位置 [英] Finding the position of a row element in a 2d ordered array

查看:40
本文介绍了在2d有序数组中查找行元素的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Java中构建一个方法,其中输入是2D整数数组,结果得到2D整数数组,其中每个元素都引用一个元素在行中的位置.让我用一个例子来解释这一点.

I need to build a method in Java where the input is a 2D array of integers and get as a result a 2D array of integers where each element makes reference to a position of an element in a row. Let's me explain that with an example.

将7x7的2D数组视为该方法的输入,如下所示:

Consider as a input for the method a 2D arrays of 7x7 as follow:

int[][] array = new int[][]{
        {280, 103, 351, 226, 451, 563, 507},
        {173, 71, 40, 100, 396, 315, 442},
        {421, 326, 210, 308, 535, 487, 549},
        {87, 165, 0, 19, 213, 405, 281},
        {25, 0, 104, 195, 298, 238, 223},
        {2, 17, 68, 0, 98, 196, 236},
        {356, 225, 454, 408, 567, 681, 604}};

我使用了一种方法,根据每行的值以递增顺序对该数组进行排序.代码如下:

I used a method to order this array in increasing order according to the values of each row. The code is the following:

public static int[][] sortRowWise (int m[][]) {
    for (int i = 0; i < m.length; i++) {
        for (int j = 0; j < m[i].length; j++) {
            for (int k = 0; k < m[i].length - j - 1; k++) {
                if (m[i][k] > m[i][k + 1]) {
                    int t = m[i][k];
                    m[i][k] = m[i][k + 1];
                    m[i][k + 1] = t;
                }
            }
        }
    }

    // printing the sorted matrix
    int[][] mR = new int[m.length][m[0].length];
    for (int i = 0; i < m.length; i++) {
        for (int j = 0; j < m[0].length; j++) {
            mR[i][j] = m[i][j];
        }
    }
    return mR;
}

输出为:

现在,我需要根据以下内容构建一个新的2D整数数组(我将在下面调用 newArray ):

Now I need to build a new 2D array of integer (I will call newArray in the following) according with:

  1. 无序"行0中的最小值.数组是103并且是与第1列相关联(然后,我需要在 newArray [0] [0] .
  2. 接下来,在无序"行中,第0行的最小值为0.数组是226并与第3列相关联(然后,我需要在 newArray [0] [3] .
  3. 每一行等等...

如前所述,该方法的最终输出必须类似于以下2d数组.任何帮助将不胜感激.

As said before, the final output of the method must be something like the following 2d array. Any help would be highly appreciated.

推荐答案

public class Foo {
    public static void main(String... args) {
        int[][] matrix = {
                {280, 103, 351, 226, 451, 563, 507},
                {173, 71, 40, 100, 396, 315, 442},
                {421, 326, 210, 308, 535, 487, 549},
                {87, 165, 0, 19, 213, 405, 281},
                {25, 0, 104, 195, 298, 238, 223},
                {2, 17, 68, 0, 98, 196, 236},
                {356, 225, 454, 408, 567, 681, 604}};
        print(matrix);
        System.out.println();
        print(transform(matrix));
    }

    public static int[][] transform(int[][] matrix) {
        class Point {
            final int col;
            final int val;

            public Point(int col, int val) {
                this.col = col;
                this.val = val;
            }
        }

        List<Queue<Point>> queues = new ArrayList<>(matrix.length);

        for (int row = 0; row < matrix.length; row++) {
            Queue<Point> queue = new PriorityQueue<>(
                    Comparator.comparingInt(point -> point.val));

            for (int col = 0; col < matrix[row].length; col++)
                queue.add(new Point(col, matrix[row][col]));

            queues.add(queue);
        }

        int[][] res = new int[matrix.length][];

        for (int row = 0; row < matrix.length; row++) {
            Queue<Point> queue = queues.get(row);
            int col = 0;
            res[row] = new int[queue.size()];

            while (!queue.isEmpty())
                res[row][col++] = queue.remove().col;
        }
        return res;
    }

    public static void print(int[][] matrix) {
        for (int row = 0; row < matrix.length; row++) {
            for (int col = 0; col < matrix[row].length; col++) {
                if (col > 0)
                    System.out.print(' ');
                System.out.format("%3d", matrix[row][col]);
            }
            System.out.println();
        }
    }
}

输出:

280 103 351 226 451 563 507
173  71  40 100 396 315 442
421 326 210 308 535 487 549
 87 165   0  19 213 405 281
 25   0 104 195 298 238 223
  2  17  68   0  98 196 236
356 225 454 408 567 681 604

  1   3   0   2   4   6   5
  2   1   3   0   5   4   6
  2   3   1   0   5   4   6
  2   3   0   1   4   6   5
  1   0   2   3   6   5   4
  3   0   1   2   4   5   6
  1   0   3   2   4   6   5

这篇关于在2d有序数组中查找行元素的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆