猿问

在 am*n 矩阵中查找特殊元素

我的问题是要在 m*n 矩阵中找到不同数量的位置元素,它们是相应行或列中的最小值或最大值。下面是我的一段代码。


static void findSpecialElement(int[][] matrix)  

{  

    for (int i = 0; i < matrix.length; i++)  

    {  

        int rowMin = matrix[i][0];               

        int colIndex = 0;           

        boolean specialElement = true;


        for (int j = 1; j < matrix[i].length; j++)

        {

            if(matrix[i][j] < rowMin)

            {

                rowMin = matrix[i][j];                     

                colIndex = j;

            }

        } 

        for (int j = 0; j < matrix.length; j++)

        {

            if(matrix[j][colIndex] > rowMin)

            {

                specialElement = false;                     

                break;

            }

        }


        if(specialElement)

        {

            System.out.println("Special Element is : "+rowMin);

        }

    }

}

例如:给定一个大小为 3*3 的矩阵,元素存储如下


1  3  4

5  2  9

8  7  6

预期输出为 7


留下5和3中的矩阵中的所有其它号码具有任一的最低或最高行和column.So,7出9号的具有最小或最大的值。


然后7是输出


请返回 -1,如果任何行或任何列有多个最小或最大元素...


我的错误就是我未能获得预期的答案7为每的问题。


扬帆大鱼
浏览 104回答 3
3回答

炎炎设计

根据更新,我认为您的问题可以简化为:计算行或列中最小值或最大值的项目。如果没问题,你的算法是错误的,因为:您正在检查列和行中的最小值(同时在两者中)你没有检查最大值您正在打印找到的号码所以,你的策略应该是这样的:在零中创建一个计数器对于矩阵中的每个项目检查他的行中是否是 min检查他的行中是否最大检查他的专栏中是否是 min检查他的列中是否最大如果一张支票没问题,增加计数器打印或返回计数器那应该对你有帮助。

呼唤远方

我认为它可以以更好=更快的方式完成,但我的O(n^2):import java.util.HashSet;import java.util.Set;public class Main {&nbsp; &nbsp; public static int[][] input = {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {1, 3, 4},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {5, 2, 9},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {8, 7, 6}&nbsp; &nbsp; };&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; int numberOfElements = 0;&nbsp; &nbsp; &nbsp; &nbsp; Set<Integer> numberOfUniqueElements = new HashSet<>();&nbsp; &nbsp; &nbsp; &nbsp; Set<Integer> specialElements = new HashSet<Integer>();&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < input.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int maxInRow = Integer.MIN_VALUE;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int minInRow = Integer.MAX_VALUE;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int maxInColumn = Integer.MIN_VALUE;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int minInColumn = Integer.MAX_VALUE;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < input[i].length; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; numberOfElements++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; numberOfUniqueElements.add(input[i][j]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (input[i][j] > maxInRow) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; maxInRow = input[i][j];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (input[i][j] < minInRow) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; minInRow = input[i][j];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (input[j][i] > maxInColumn) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; maxInColumn = input[j][i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (input[j][i] < minInColumn) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; minInColumn = input[j][i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; specialElements.add(minInRow);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; specialElements.add(maxInRow);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; specialElements.add(minInColumn);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; specialElements.add(maxInColumn);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (numberOfUniqueElements.size() != numberOfElements) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("-1");&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(specialElements.size());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
随时随地看视频慕课网APP

相关分类

Java
我要回答