这是我参与2022首次更文挑战的第1天,活动详情查看:2022首次更文挑战
思路: 同行最小且同列最大的数只有一个, 找到每行最小的数, 每列最大的数, 然后遍历找相同的数即可
class Solution {
public List<Integer> luckyNumbers (int[][] matrix) {
int m = matrix.length;
int n = matrix[0].length;
List<Integer> res = new ArrayList<>();
int[] row = new int[m];
int[] col = new int[n];
// 给行最小值数组填充值
Arrays.fill(row,Integer.MAX_VALUE);
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
row[i] = Math.min(matrix[i][j], row[i]);
col[j] = Math.max(matrix[i][j], col[j]);
}
}
for (int i = 0; i < row.length; i++) {
for (int j = 0; j < col.length; j++) {
if (row[i] == col[j]) res.add(row[i]);
}
}
return res;
}
}