The sword refers to the second edition of the offer interview question 29: print the matrix clockwise (java)

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

Analysis:
When faced with a complex problem, we can use graphics to help us think. Since it is printed sequentially from the outer circle to the inner circle, we can imagine the matrix as several circles, as shown in the figure. We can print the matrix in a loop, one cycle at a time.
write picture description here
Next, analyze the conditions for the end of the loop. Suppose the number of rows of this matrix is ​​rows and the number of columns is columns. The coordinates of the upper-left element of the first circle are (0, 0), the coordinates of the upper-left corner of the second circle are (1, 1), and so on. We noticed that the row and column labels in the coordinates of the upper left corner are always the same, so we can select a circle whose upper left corner is (start, start) in the matrix as the target of our analysis.
For a 5*5 matrix, there is only one number in the last circle, and the corresponding coordinate is (2, 2). We find that 5>2*2. For a 6*6 matrix, there are 4 numbers in the last circle, and the coordinates of the upper left corner are still (2, 2). We find that 6>2*2 still holds. So we can conclude that the conditions for continuing the loop are columns>startX*2 and rows>startY*2.

Next we consider how to print the function of a circle. As shown in the figure, we can divide printing a circle into four steps: the first step is to print a line from the right, the second step is to print a line from top to bottom, the third step is to print a line from right to left, and the fourth step is from Print one column from bottom to top. At each step we can print a row or a column with a loop based on the starting coordinates.

It's worth noting, though, that the last lap may degenerate into only one row, only one column, or even just one number, so printing such a lap no longer requires four steps. The figure is a few examples of degradation, and printing a circle only takes three steps, two steps, or even only one step, respectively.
write picture description here

code show as below:

/**
 * 顺时针打印数组
 */
public class PrintMatrix {

    /**
     * 顺时针打印数组
     * 
     * @param numbers
     *            二维数组
     * @param rows
     *            行数
     * @param columns
     *            列数
     */
    public void printMatrixClockwisely(int[][] numbers, int rows, int columns) {
        if (numbers == null || rows <= 0 || columns <= 0) {
            return;
        }
        int start = 0;
        while (start * 2 < columns && start * 2 < rows) {
            printMatrixInCircle(numbers, rows, columns, start);
            start++;
        }
    }

    public void printMatrixInCircle(int[][] numbers, int rows, int columns,int start) {
        //终止列号
        int endX = columns - 1 - start;
        //终止行号
        int endY = rows - 1 - start;
        //从左至右打印一行
        for(int i = start; i <= endX; i++){
            System.out.print(numbers[start][i]+"  ");
        }
        //从上到下打印一列
        if(endY > start){
            for(int i = start+1; i<= endY; i++){
                System.out.print(numbers[i][endX] + "  ");
            }
        }
        //从右至左打印一行
        if(endY > start && endX > start){
            for(int i  = endX-1 ; i>= start; i--){
                System.out.print(numbers[endY][i] + "  ");
            }
        }
        //从下到上打印一列
        if(endY - 1 > start && endX > start){
            for(int i = endY-1; i >= start+1 ; i--){
                System.out.print(numbers[i][start] + "  ");
            }
        }
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325728154&siteId=291194637