The sword refers to offer--20. Print the matrix clockwise

Question: Input a matrix and print out each number in clockwise order from outside to inside

Analysis: You can imagine the matrix as several circles, use a loop to print the matrix, and print one circle in the matrix at a time, as shown on the left. Let the matrix rows and columns be columns, the coordinates of the upper left corner of the first circle of printing are (0,0), the coordinates of the upper left corner of the second circle are (1,1), and so on. Note the row and column labels in the coordinates of the upper left corner. It is always the same, selecting a circle in the upper left corner of the matrix (start, start) as the analysis target to find that the condition for the loop to continue is columns>startX*2 and rows>startY*2.


How to print a circle is divided into four steps, namely: 1. Print one line from left to right; 2. Print one column from top to bottom; 3. Print one line from right to left; 4. Print one column from bottom to top, pay attention It is the last circle that may degenerate into only one row, only one column or only one number, as shown in the figure on the right, according to the preconditions of each step during printing, the loop can be analyzed and written, the code is as follows

import java.util. *;
public class wr20printMatrix {
	static ArrayList<Integer> list=new ArrayList<>();
	public static ArrayList<Integer> printMatrix(int [][]matrix){
		int rows=matrix.length;//行
		int columns=matrix[0].length;//列
		int start=0;
		while(rows>start*2 && columns>start*2){
			printMatrixInCircle(matrix,rows,columns,start);
			start++;
		}
		return list;
	}
	
	public static void printMatrixInCircle(int [][]matrix,int rows,int columns,int start){
		int endRow=columns-start-1;//One line to the end, indicating the number of columns
		int endColumns=rows-start-1;//One column to the end, there are several rows
// 1. Print a line from left to right
// It takes at least one step to print a circle
		for(int i=start;i<=endRow;i++){
			list.add(matrix[start][i]);
		}
// 2. Print a column from top to bottom
// The premise of the second step is that the ending line number is greater than the starting line number
		if(start<endColumns){
			for(int i=start+1;i<=endColumns;i++){
				list.add(matrix[i][endRow]);
			}
		}
// 3. Print a line from right to left
// The premise of the third step is that there are at least two rows and two columns in the circle, that is, the ending row number is greater than the starting row number and the ending column number is greater than the starting column number
		if(start<endRow && start<endColumns){
			for(int i=endRow-1;i>=start;i--){
				list.add(matrix[endColumns][i]);
			}
		}
// 4. Print a column from bottom to top
// The premise of the fourth step is that there are at least three rows and two columns, that is, the ending row number is 2 greater than the starting row number and the ending column number is greater than the starting column number
		if(start<endColumns-1 && start<endRow){
			for(int i=endColumns-1;i>=start+1;i--){
				list.add(matrix[i][start]);
			}
		}
	}
	public static void main(String []args){
		int [][]array={{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};
		ArrayList<Integer> result=printMatrix(array);
		for(int i:result){
			System.out.print(i+" ");
		}
	}

}

Guess you like

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