熊熊出没 发表于 2025-3-16 12:32:04

leetcode top100矩阵题73.54.48.240

73. 矩阵置零

class Solution {
   public void setZeroes(int[][] matrix) {
       int line = matrix.length;
       int col = matrix.length;
       List<Integer> changeLines = new ArrayList<Integer>();
       List<Integer> changeCols = new ArrayList<Integer>();
       for(int i = 0; i < line; i++) {
           for(int j = 0; j < col; j++) {
               if (matrix == 0) {
                   changeLines.add(i);
                   changeCols.add(j);
             }
         }
     }

       for(int i = 0; i < line; i++) {
           if (changeLines.contains(i)) {
               Arrays.fill(matrix, 0);
         }
           for(int j = 0; j < changeCols.size(); j++) {
               matrix = 0;
         }
           
     }
       return;
 }
} 54. 螺旋矩阵

class Solution {
   private static final int[][] DIRS = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
   public List<Integer> spiralOrder(int[][] matrix) {
       int line = matrix.length;
       int col = matrix.length;
       int size = line * col;
       List<Integer> ans = new ArrayList<>(line * col);
       int i = 0;
       int j = -1;
       for(int di = 0; ans.size() < size; di = (di + 1) % 4) {
           for(int k = 0; k < col; k++) {
               i += DIRS;
               j += DIRS;
               ans.add(matrix);
         }
           int tmp = col;
           col = line - 1;
           line = tmp;
     }
       return ans;
 }
} 48. 旋转图像

class Solution {
   public void rotate(int[][] matrix) {
       int lines = matrix.length;
       int cols = matrix.length;
       int[][] copy = new int;
       for(int i = 0; i < lines; i++) {
           for(int j = 0; j < cols; j++) {
               copy = matrix;
         }
     }
       for(int i = 0; i < lines; i++) {
           for(int j = 0; j < cols; j++) {
               matrix = copy;
         }
     }
       return;
 }
} 240. 搜索二维矩阵Ⅱ


https://i-blog.csdnimg.cn/img_convert/fa31749fe04d1173daba288d167928bb.png
class Solution {
   public boolean searchMatrix(int[][] matrix, int target) {
       int m = matrix.length;
       int n = matrix.length;
       for(int i = 0; i < m; i++) {
           if (matrix < target) {
               continue;
         } else if (matrix == target) {
               return true;
         }
           for(int j = n - 1; j >= 0; j--) {
               if (matrix > target) {
                   continue;
             } else if (matrix == target) {
                   return true;
             } else {
                   break;
             }
         }
     }
       return false;
 }
}
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: leetcode top100矩阵题73.54.48.240