Sword finger offer: print matrix clockwise (Java)

Keywords: Java

1. 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 4 X 4 matrix: 1 23 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.

2. way of thinking

Clockwise printing matrix is to print from the outside to the inside in a circle. You only need to print the outermost layer every time, and then cycle in.
So focus on the coordinates of the two elements in the upper left corner and the lower right corner to determine the printing range. (from macro to micro)

import java.util.ArrayList;
public class Solution {
    public ArrayList<Integer> printMatrix(int [][] matrix) {
       ArrayList<Integer> res = new ArrayList<>();//New collection to store results
        int startRow = 0;    //Row coordinates of the upper left element
        int startCol = 0;    //Column coordinates of the upper left element
        int endRow = matrix.length-1;    //Row coordinates of elements in the lower right corner
        int endCol = matrix[0].length-1;    //Column coordinates of the lower right element
        //This while loop is a step forward from the top left corner to the bottom right corner after the outer circle is printed each time, until the top left corner and the bottom right corner are in the same position.
        while(startRow <= endRow && startCol <= endCol){
            //Here is to call the method to print the outermost matrix elements and save them to an ArrayList collection. After each execution, the ArrayList will be added to the res collection as a whole for the final result.
            res.addAll(printMatrixClockwise(matrix, startRow++, startCol++, endRow--, endCol--));
        }
        return res;    //Save the results to the List collection in the same order of storage and retrieval.
    }
    
    //This method is used to print the outermost elements and save them in the collection.
    public ArrayList<Integer> printMatrixClockwise(int[][] m, int startRow, int startCol, int endRow, int endCol){
        ArrayList<Integer> temp = new ArrayList<>();//Define an ArrayList to temporarily store the outermost elements of the print
        if(startRow == endRow){    //Determine if there is only one line
            for(int i = startCol; i <= endCol; i++){    //It's a one-dimensional array. Just put all the elements in the temp set.
                temp.add(m[startRow][i]);
            }
        }else if(startCol == endCol){    //In the case of only one column, it is the same as in the case of only one row.
            for(int i = startRow; i <=endRow; i++){
                temp.add(m[i][startCol]);
            }
        }else{    //More than one line and one column, print the outermost layer.
            int curRow = startRow;    //curRow record current row
            int curCol = startCol;    //Current column of curCol record
            while(curCol != endCol){    //If it is not the last column, it will be printed from left to right and saved in the collection.
                temp.add(m[curRow][curCol++]);
            }
            while(curRow != endRow){    //If it's not the last line, print it from top to bottom and save it in the collection.
                temp.add(m[curRow++][curCol]);
            }
            while(curCol != startCol){    // When the element reaches the lower right corner, it should go left, that is, when the column at the current position is not the starting column, it will decrease.
                temp.add(m[curRow][curCol--]);//Save each element to the collection
            }
            while(curRow != startRow){    //To the element position in the lower left corner, go up, so the current position row decrements.
                temp.add(m[curRow--][curCol]);//Save each element to the collection
            }
        }
        return temp;    //Print the outermost layer, save it to the collection, and return the collection.
    }
}

Posted by iFlex on Wed, 16 Oct 2019 15:36:18 -0700