目前网站开发趋势,免费咨询律师微信公众号,jsp 做网站需要什么,网站不备案有什么影响文章目录 顺时针打印二维数组0所在的行列清零 顺时针打印二维数组 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(i… 文章目录 顺时针打印二维数组0所在的行列清零 顺时针打印二维数组 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(leftUpRowrightDownRow leftUpCol rightDownCol){int r leftUpRow,c leftUpCol;while(crightDownCol){System.out.print(matrix[r][c] );}r;c rightDownCol;while(rrightDownRow){System.out.print(matrix[r][c] );}c--;r rightDownRow;while(cleftUpCol){System.out.print(matrix[r][c--] );}r--;c leftUpCol;while(rleftUpRow){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;imatrix.length;i){for(int j 0;jmatrix[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;//记录哪些行出现了0int[] rowRecord new int[M];int[] colRecord new int[N];for(int i 0;iM;i){for(int j 0;jN;j){if(matrix[i][j] 0){rowRecord[i] 1;colRecord[j] 1;}}}for(int row 0;row N;row){for(int col 0;colM;col){if(rowRecord[row] 1 || colRecord[col] 1){matrix[row][col] 0;}}}}}