public static int[] getMinimumSymmetricOfEveryRow (int [][] A)
{
int min = Integer.MAX_VALUE;
int[] arr=new int[A.length];
for (int i = 0; i < A.length; i++){
for (int j = 0; j < A[i].length; j++){
if (A[i][j] < min && isSymmetric(A[i][j])){
arr[i] = A[i][j];
}
}
}
return arr;
}
这是我的函数,从2D数组的每一行返回最小对称元素的数组。它对数组[n][n]工作得很好,但对数组[m][n]却不行…(它返回m元素,而不是n)。我不知道是什么问题??
假设输入数组的格式是[row][column]和方法应该是getMinimumSymmetricOfEveryColumn你可以写你的方法为:
public static int[] getMinimumSymmetricOfEveryColumn(int[][] matrix) {
int[] columnMinimum = new int[matrix[0].length];
for (int colimnIndex = 0; colimnIndex < matrix[0].length; colimnIndex++){
int currentMinimum = Integer.MAX_VALUE;
for (int rowIndex = 0; rowIndex < matrix.length; rowIndex++){
int currentElement = matrix[rowIndex][colimnIndex];
if (currentElement < currentMinimum && isSymmetric(currentElement)){
columnMinimum[colimnIndex] = currentElement;
}
}
}
return columnMinimum;
}