順時針打印二維數組

package 每日算法學習打卡.算法打卡.七月份.七月二十六號;public class test1 {public static void main(String[] args) {int[][] matrix = {{1,2},{5,6},{9,10},{13,14},};print(matrix);}static void print(int[][] matrix){int leftUpRow =0,leftUpCol =0,rightDownRow = matrix.length-1,rightDownCol = matrix[0].length-1;while(leftUpRow<=rightDownRow && leftUpCol <= rightDownCol){int r = leftUpRow,c = leftUpCol;while(c<=rightDownCol){System.out.print(matrix[r][c++]+" ");}r++;c = rightDownCol;while(r<=rightDownRow){System.out.print(matrix[r++][c]+" ");}c--;r= rightDownRow;while(c>=leftUpCol){System.out.print(matrix[r][c--]+" ");}r--;c = leftUpCol;while(r>leftUpRow){System.out.print(matrix[r--][c]+" ");}leftUpCol++;leftUpRow++;rightDownCol--;rightDownRow--;}}
}
0所在的行列清零

package 每日算法學習打卡.算法打卡.七月份.七月二十六號;public class test2 {public static void main(String[] args) {int[][] matrix = {{1,2,3,4,100},{5,6,7,0,101},{9,0,11,12,102},{13,14,15,16,103},{104,105,106,106,108},};solve(matrix);for(int i =0;i<matrix.length;i++){for(int j =0;j<matrix[0].length;j++){System.out.print(matrix[i][j]+" ");}System.out.println();}}static void solve(int[][] matrix){int M = matrix.length;int N = matrix.length;int[] rowRecord = new int[M];int[] colRecord = new int[N];for(int i =0;i<M;i++){for(int j =0;j<N;j++){if(matrix[i][j] == 0){rowRecord[i] =1;colRecord[j] =1;}}}for(int row = 0;row < N;row++){for(int col = 0;col<M;col++){if(rowRecord[row] ==1 || colRecord[col] == 1){matrix[row][col] =0;}}}}}