我的问题是要在 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为每的问题。
炎炎设计
呼唤远方
相关分类