如何在二维矩阵的子矩阵中查找重复项并进行比较

我有一个问题。谁能帮我找到子矩阵中的重复项? 我有一个代码可以在2D矩阵中找到子矩阵,但是找不到重复项。我以为将值压入堆栈(因为在分配中我应该使用堆栈),在每个子矩阵中找到所有重复项,然后将它们进行比较,但我真的不知道该怎么做。如果有人帮助我完成此程序,我将不胜感激。

我的代码:

public static void main(String[] args) throws Exception {
        int[][] data = new int[3][3];
        Random random = new Random();
        for(int i=0; i<data.length;i++)
        {
            for(int j=0; j<data.length; j++)
            {
                data[i][j] = random.nextInt(10);
            }
        }
        printSubMatrix(data);
    }

private static void printSubMatrix(int[][] mat) {
        int rows=mat.length;
        int cols=mat[0].length;
        Stack _stack = new Stack();
        //prints all submatrix greater than or equal to 2x2
        for (int subRow = rows; subRow >= 2; subRow--) {
            int rowLimit = rows - subRow + 1;
            for (int subCol = cols; subCol >= 2; subCol--) {
                int colLimit = cols - subCol + 1;
                for (int startRow = 0; startRow < rowLimit; startRow++) {
                    for (int startCol = 0; startCol < colLimit; startCol++) {
                        for (int i = 0; i < subRow; i++) {
                            for (int j = 0; j < subCol; j++) {
                                System.out.print(mat[i + startRow][j + startCol] + " ");
                                _stack.push(mat[i+startRow][j+startCol]);
                            }
                            System.out.print("\n");
                        }
                        System.out.print("\n");
                    }
                }
            }
        }
    System.out.printf(_stack.toString().replaceAll("\\[","").replaceAll("]",""));
    }
vcaonimav 回答:如何在二维矩阵的子矩阵中查找重复项并进行比较

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3100530.html

大家都在问