题目

类型:数组
image.png

解题思路

创建两个数组 row 和 col 用于进行预处理,row[x] 含义为第 x 行的最小值,col[y] 为第 y 列的最大值。

代码

  1. class Solution {
  2. int N = 55;
  3. int[] row = new int[N], col = new int[N];
  4. public List<Integer> luckyNumbers (int[][] mat) {
  5. int n = mat.length, m = mat[0].length;
  6. for (int i = 0; i < n; i++) {
  7. row[i] = 100001;
  8. for (int j = 0; j < m; j++) {
  9. row[i] = Math.min(row[i], mat[i][j]);
  10. col[j] = Math.max(col[j], mat[i][j]);
  11. }
  12. }
  13. List<Integer> ans = new ArrayList<>();
  14. for (int i = 0; i < n; i++) {
  15. for (int j = 0; j < m; j++) {
  16. int t = mat[i][j];
  17. if (t == row[i] && t == col[j]) ans.add(t);
  18. }
  19. }
  20. return ans;
  21. }
  22. }