Sword finger offer print matrix clockwise

Title Description
Enter a matrix and print out each number in a clockwise order from the outside to the inside. For example, if you enter the following matrix: 1 23 4 5 6 7 8 9 10 11 12 13 14 15 16, the numbers 1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10 will be printed out in turn

Idea: the method of clockwise printing is very simple, as long as the cycle can be printed according to the circle. A count count is required for each print cycle to find the starting point of the next cycle. The flag to keep the loop is rows, and the length of columns is more than twice the number of turns, that is to say

rows > 2 * cicle && columns > 2 * cicle

In addition, the most important thing is to consider special input, including: empty array, array length is 0, two-dimensional array has only one row or one column, the number of rows and columns is not equal (for example, 5 rows and 3 columns, 3 rows and 5 columns). Among them, the number of different ranks is the most error prone point. It's easy to see that the vertical line is printed from top to bottom, and the vertical line is printed from bottom to top.

Reference code

import java.util.ArrayList;

public class PrintMatrixAAAA {
     public static ArrayList<Integer> printMatrix(int [][] matrix) {
           if(matrix == null)
               return null;
           if (matrix.length == 0)
               return null;
           int rows = matrix.length,columns = matrix[0].length,cicle = 0;
           ArrayList<Integer> printResult = new ArrayList<>();
           while (rows > 2 * cicle && columns > 2 * cicle) {
               printCicle(matrix,cicle,printResult);
               cicle ++;
        }
           return printResult;
    }

    private static void printCicle(int[][] matrix, int cicle,ArrayList<Integer> list) {
        for(int i = cicle;i < matrix[0].length - cicle;i++) {
            list.add(matrix[cicle][i]);
        }
        for(int i = cicle + 1;i < matrix.length - cicle;i++) {
            list.add(matrix[i][matrix[0].length - 1 - cicle]);
        }
        for(int i = matrix[0].length - 2 - cicle;i >= cicle;i--) {
            if(matrix.length - 1 - cicle != cicle)//Prevent the same line from printing back and forth
                list.add(matrix[matrix.length - 1 - cicle][i]);
        }
        for(int i = matrix.length - 2 - cicle;i > cicle;i--) {
            if (matrix[0].length - 1 - cicle!= cicle) {//Prevent the same column from printing back and forth
                list.add(matrix[i][cicle]);
            }
        }
    }

    //Main program for testing
    public static void main(String[] args) {
        int row = 5,column = 3,count = 1;
        int[][] matrix  = new int[row][column];
        for(int  i = 0; i < row;i++) {
            for(int j = 0;j < column;j++) {
                matrix[i][j] = count++;
                System.out.print(matrix[i][j] + ",");
            }
            System.out.println(" ");
        }
        ArrayList<Integer> arrayList = printMatrix(matrix);
        for (Integer integer : arrayList) {
            System.out.print(String.valueOf(integer) + " ");
        }
    }
}

Tags: Java

Posted on Thu, 13 Feb 2020 15:33:01 -0500 by Wizard4U