转圈打印矩阵 您所在的位置:网站首页 转圈打印矩阵 转圈打印矩阵

转圈打印矩阵

2023-11-26 01:16| 来源: 网络整理| 查看: 265

给定一个整型矩阵matrix,请按照转圈的方式打印它。 例如: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 打印结果为:1,2,3,4,8,12,16,15,14,13,9, 5,6,7,11, 10 【要求】 额外空间复杂度为O(1)。

Code //coding=utf8 /***************************************************** @Author: Alex @Created Time : Tue 27 Aug 2019 08:48:30 AM CST @File Name: main.java @Blog: https://blog.csdn.net/weixin_43336281 ****************************************************/ public class main{ public static void PrintMatrix(int[][] matrix, int leftRow, int leftColumn, int rightRow, int rightColumn) { if (leftRow == rightRow) { for (int i = leftColumn; i int curRow = leftRow, curColumn = leftColumn; for(; curColumn != rightColumn; curColumn++) System.out.println(matrix[leftRow][curColumn] + " "); for(; curRow != rightRow; curRow++) System.out.println(matrix[curRow][rightColumn]); for(; curColumn != leftColumn; curColumn--) System.out.println(matrix[rightRow][curColumn]); for(; curRow != leftRow; curRow--) System.out.println(matrix[curRow][leftColumn]); } } public static void CirclePrintingMatrix(int [][] matrix){ int leftRow = 0, leftColumn = 0; int rightRow = matrix.length - 1, rightColumn = matrix[0].length - 1; while(leftRow {1,2,3,4}, {5,6,7,8}, {9,10,11,12}, {13,14,15,16} }; CirclePrintingMatrix(matrix); } }


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有