摘要本文解析 LeetCode 热题 100 中矩阵部分的题目。矩阵73 矩阵置零核心逻辑通过两个布尔数组分别记录需要置零的行和列先遍历矩阵标记所有含 0 的行和列再根据标记结果分别对行、列批量置零最终实现原地修改矩阵的目标核心是标记 - 置零的分步逻辑。关键步骤初始化标记数组创建两个布尔数组rowZero长度为矩阵行数 m和colZero长度为矩阵列数 n分别用于记录哪些行 / 列需要置零初始值均为 false。遍历标记行 / 列逐行逐列遍历整个矩阵若遇到元素matrix[i][j] 0则将rowZero[i]设为 true标记第 i 行需置零、colZero[j]设为 true标记第 j 列需置零。置零标记行遍历rowZero数组若rowZero[i] true则将矩阵第 i 行的所有元素置为 0。置零标记列遍历colZero数组若colZero[j] true则将矩阵第 j 列的所有元素置为 0。class Solution { public void setZeroes(int[][] matrix) { int m matrix.length; int n matrix[0].length; boolean[] rowZero new boolean[m]; // 记录哪些行需要置零 boolean[] colZero new boolean[n]; // 记录哪些列需要置零 // 第一步遍历矩阵标记行/列 for (int i 0; i m; i) { for (int j 0; j n; j) { if (matrix[i][j] 0) { rowZero[i] true; colZero[j] true; } } } // 第二步置零标记的行 for (int i 0; i m; i) { if (rowZero[i]) { for (int j 0; j n; j) { matrix[i][j] 0; } } } // 第三步置零标记的列 for (int j 0; j n; j) { if (colZero[j]) { for (int i 0; i m; i) { matrix[i][j] 0; } } } } }54 螺旋矩阵核心逻辑通过边界收缩法控制螺旋的方向和范围 —— 定义上、下、左、右四个边界按 右→下→左→上 的顺序循环遍历矩阵元素每完成一个方向就收缩对应边界直到所有元素遍历完毕并收集到结果列表中。关键步骤初始化边界与结果定义 top 0、bottom 矩阵行数 - 1、left 0、right 矩阵列数 - 1初始化空的结果列表 res用于存储遍历后的元素。循环遍历边界收缩向右遍历遍历 [left, right] 列收集 top 行的所有元素到 res完成后 top上边界下移向下遍历遍历 [top, bottom] 行收集 right 列的所有元素到 res完成后 right--右边界左移向左遍历需判断上下边界是否交叉若 top ≤ bottom遍历 [right, left] 列收集 bottom 行的所有元素到 res完成后 bottom--下边界上移向上遍历需判断左右边界是否交叉若 left ≤ right遍历 [bottom, top] 行收集 left 列的所有元素到 res完成后 left左边界右移终止条件当 top bottom 或 left right 时所有元素遍历完毕返回结果列表 res。class Solution { public ListInteger spiralOrder(int[][] matrix) { ListInteger res new ArrayList(); if (matrix null || matrix.length 0) return res; int top 0, bottom matrix.length - 1; int left 0, right matrix[0].length - 1; while (top bottom left right) { // 向右遍历 for (int j left; j right; j) { res.add(matrix[top][j]); } top; // 向下遍历 for (int i top; i bottom; i) { res.add(matrix[i][right]); } right--; // 向左遍历判断边界 if (top bottom) { for (int j right; j left; j--) { res.add(matrix[bottom][j]); } bottom--; } // 向上遍历判断边界 if (left right) { for (int i bottom; i top; i--) { res.add(matrix[i][left]); } left; } } return res; } }48 旋转图像核心逻辑通过 矩阵转置 每行左右翻转 两步操作实现原地顺时针旋转 90 度 —— 先将矩阵行和列互换完成转置再对转置后的每一行进行左右翻转无需额外矩阵空间仅通过元素交换完成原地修改。关键步骤转置矩阵遍历矩阵上三角区域i 从 0 到 n-1j 从 i 到 n-1交换matrix[i][j]和matrix[j][i]完成后矩阵行和列互换翻转每行遍历矩阵每一行i 从 0 到 n-1对当前行遍历前半部分列j 从 0 到 n/2-1交换matrix[i][j]和matrix[i][n-1-j]完成后该行元素左右翻转。public class RotateImage { public void rotate(int[][] matrix) { int n matrix.length; // 第一步转置矩阵 for (int i 0; i n; i) { for (int j i; j n; j) { // 交换 matrix[i][j] 和 matrix[j][i] int temp matrix[i][j]; matrix[i][j] matrix[j][i]; matrix[j][i] temp; } } // 第二步对称交换 for (int i 0; i n; i) { for (int j 0; j n / 2; j) { int temp matrix[i][j]; matrix[i][j] matrix[i][n - 1 - j]; matrix[i][n - 1 - j] temp; } } } }240 搜索二维矩阵 Ⅱ核心逻辑通过 边界收缩法 从矩阵右上角起始搜索 —— 利用矩阵 每行升序、每列升序 的特性以右上角为起始点每次比较可排除一行或一列逐步收缩搜索边界无需额外空间且时间复杂度最优高效判断。关键步骤边界初始化处理空矩阵边界情况矩阵为 null / 行数为 0 / 列数为 0 时直接返回 false定义行数 m 矩阵长度、列数 n 矩阵首行长度初始化搜索起始点为矩阵右上角row0coln-1边界收缩搜索循环判断 row m 且col 0未越界取当前位置值 currentmatrix [row][col]若 current target找到目标值直接返回 true若 current target当前列所有元素均大于目标值左移列边界col--排除当前列若 current target当前行所有元素均小于目标值下移行边界row排除当前行终止返回当循环因边界越界终止时说明未找到目标值返回 false。public class SearchMatrixII { public boolean searchMatrix(int[][] matrix, int target) { if (matrix null || matrix.length 0 || matrix[0].length 0) {return false;} int m matrix.length; int n matrix[0].length; int row 0; int col n - 1; while (row m col 0) { int current matrix[row][col]; if (current target) { return true; } else if (current target) { col--; } else { row; } } return false; } }恭喜你学习完成✿