我看leetcode里面的矩阵题目有4道题作为一个新手完全不熟悉哎让我们来看看他们怎么事LeetCode 73.矩阵置零73. 矩阵置零 - 力扣LeetCode这里就不放图了。题目描述还挺好理解就是啥叫原地算法搜一下--- 哦就是不开一个新的呗非得整这个学名哈哈哈那么开始思路考虑遍历矩阵看哪些行有0看那些列有0记录一下再遍历一遍把对应行列置零。可是会产生额外空间Omn;为了避免好吧我不装了直接看题解开背总体思路就是我们借用矩阵自己的第一列记录哪些行要清零 第一行记录哪些列要清零[[1, 1, 1],[1, 0, 1], -------------》 a[1][1] 0 ------》a[1][0] 0;[1, 1, 1] a[0][1] 0;][ [1, 0, 1],[0, 0, 1],[1, 1, 1] ]懂我意思吗那第一列和第一行因为记录变了怎么办呢他们当标记区了呀他们有没有0怎么判断呢所以得弄两个变量记录一下boolean r0 false; // 第一行原本有没有 0boolean c0 false; // 第一列原本有没有 0r0就是row 0就是第一行喽c0就是column 0 就是第一列喽直接写代码class Solution { public void setZeroes(int[][] a) { int m a.length; int n a[0].length; boolean r0 false; boolean c0 false; // 看第一行有没有 0 for (int j 0; j n; j) { if (a[0][j] 0) { r0 true; break; } } // 看第一列有没有 0 for (int i 0; i m; i) { if (a[i][0] 0) { c0 true; break; } } // 用第一行和第一列做标记 for (int i 1; i m; i) { for (int j 1; j n; j) { if (a[i][j] 0) { a[i][0] 0; a[0][j] 0; } } } // 根据标记把中间区域置零 for (int i 1; i m; i) { for (int j 1; j n; j) { if (a[i][0] 0 || a[0][j] 0) { a[i][j] 0; } } } // 如果第一行原本有 0第一行全置零 if (r0) { for (int j 0; j n; j) { a[0][j] 0; } } // 如果第一列原本有 0第一列全置零 if (c0) { for (int i 0; i m; i) { a[i][0] 0; } } } }好的思路看明白了一定一定一定要默写偶照着写进步很慢的。注意字母的准确注意一下边界遍历从1开始先遍历最后再根据r0,c0置零不要写错了挺有意思的就是太长了代码。LeetCode 54 螺旋矩阵54. 螺旋矩阵 - 力扣LeetCode题目描述一目了然直接看题解把就是维护4个边界上下左右上面走完了就top然后right--;bottom--;left;判断标准top bottom left right还有行可以走并且还有列可以走。只有都成立才能证明还有没遍历的区域。走完两个一定要判断一下很重要。看代码把class Solution { public ListInteger spiralOrder(int[][] matrix) { ListInteger ans new ArrayList(); int m matrix.length;//行数 int n matrix[0].length;//列数 int top 0,bottom m-1,left 0,right n -1; while(topbottomrightleft){ for(int j left;jright;j){ ans.add(matrix[top][j]); } top; for(int i top;ibottom;i){ ans.add(matrix[i][right]); } right--; if(topbottom){//这里一定要判断 for(int j right;jleft;j--){ ans.add(matrix[bottom][j]); } bottom--; } if(leftright){ for(int i bottom;itop;i--){ ans.add(matrix[i][left]); } left; } } return ans; } }很考验基本功偶一定不要着急要细心急死我了下两道题明天再学我去复习一下redis八股。