Implementation of Clockwise Printing Matrix by java

  • 2021-07-01 07:28:26
  • OfStack

This article example for everyone to share java to achieve clockwise printing matrix specific code, for your reference, the specific content is as follows

Title:

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

1 2 3 4

5 6 7 8

9 10 11 12

13 14 15 16

Method 1:

This paper introduces a matrix processing method: matrix cycle processing. In the matrix, the coordinates of the upper left corner (tR, tC) and the coordinates of the lower right corner (dR, dC) can represent a submatrix, such as the matrix in the title. When (tR, tC) = (0, 0), (dR, dC) = (3, 3), the submatrix represented is the whole matrix, then the outermost part of this submatrix is:

1 2 3 4

5 8

9 12

13 14 15 16

Print the outermost layer of this submatrix clockwise, and when (tR, tC) = (0, 0), (dR, dC) = (3, 3), the printed results are: 1, 2, 3, 4, 8, 12, 16, 15, 14, 13, 9, 5. Next, when tR and tC are added by 1, dR and dC are subtracted by 1, respectively, that is, (tR, tC) = (1, 1), (dR, dC) = (2, 2), the submatrix at this time is:

6 7

10 11

Then print this matrix clockwise, and the result is 6, 7, 11, 10. Add 1 to tR and tC, and subtract 1 from dR and dC, that is, (tR, tC) = (2, 2), (dR, dC) = (1, 1). If the upper-left coordinates are to the right or below the lower-right coordinates (i.e. tR > dR ||tC > dC), stop and all results that have been printed are the required print results.


package Array;
 
import java.util.ArrayList;
import java.util.List;
 
public class spiralOrder {
 /**
 * @param matrix: a matrix of m x n elements
 * @return: an integer list
 */
 // Circular printing matrix 
 // Upper left corner point (tR,tC) , lower right corner (dR,dC)
 public List<Integer> spiralOrder(int[][] matrix) {
 // write your code here
 List<Integer> list=new ArrayList<>();
 if(matrix==null||matrix.length==0) return list;
 int dR=matrix.length-1;
 int dC=matrix[0].length-1;
 int tR=0;
 int tC=0;
 while(tR<=dR && tC<=dC ){
  PrintMatrix(matrix,list,tR++,tC++,dR--,dC--);
 }
 return list;
 }
 
 private void PrintMatrix(int[][] matrix,List<Integer> list,int tR,int tC,int dR,int dC){
 if(tR==dR){ //  Submatrix only 1 Row 
  for(int i=tC;i<=dC;i++)
  list.add(matrix[tR][i]);
 }else if(tC==dC){ //  Submatrix only 1 Column 
  for(int i=tR;i<=dR;i++)
  list.add(matrix[i][tC]);
 }else{
  int curC=tC;
  int curR=tR;
  while(curC!=dC){
  list.add(matrix[tR][curC]);
  curC++;
  }
  while(curR!=dR){
  list.add(matrix[curR][dC]);
  curR++;
  }
  while (curC!=tC){
  list.add(matrix[dR][curC]);
  curC--;
  }
  while(curR!=tR){
  list.add(matrix[curR][tC]);
  curR--;
  }
 }
 }
 
 public static void main(String[] args) {
 spiralOrder spiralOrder=new spiralOrder();
 int[][] matrix={};
 System.out.println(spiralOrder.spiralOrder(matrix));
 }
}

Method 2:

Analysis: If the upper left corner of the matrix is (0, 0), then the starting points of every 1 turn are (0, 0), (1, 1)... It can be observed that if the 2*2 matrix only prints 1 turn, the 3*3 matrix prints 2 turns, and the 3*2 matrix prints 1 turn, so there is col > count*2 & & row > count*2, count starts at 0.

For each turn of printing, the start line number and column number are count, the end line number endrow=row-1-count, and the end column number endcol=col-1-count.

Step 1 Printing from left to right is required, circulating with line numbers count and column numbers count incrementing through endrow.

Step 2 Printing from top to bottom satisfies the condition that the end line number is greater than the start line number endrow > count, circular printing, line number count+1 incremented through endrow, column number endcol.

Step 3 Print from right to left satisfies the condition that the end line number of step 2 is greater than the start line number endrow > count and the stop column number is greater than the start column number endcol > count, circular printing, line number endrow, column number endcol-1 to count decrement.

Step 4 Printing from bottom to top satisfies the condition that the end column number is greater than the start column number endcol > start and the end line number is at least 2 larger than the start line number, i.e. endrow-count > 1. Cyclic printing, line number endrow-1 to count+1 decreasing, column number count.


import java.util.ArrayList;
public class Solution {
 public ArrayList<Integer> result=new ArrayList<>();
 public ArrayList<Integer> printMatrix(int [][] matrix) {
  int row=matrix.length;
  int col=matrix[0].length;
  if(matrix==null || row<0 || col<0){
   return null;
  }
  int count=0;
  while(col>count*2 && row>count*2){
   PrintCircle(matrix,col,row,count);
   count++;
  }
  return result;
 }
 
 public void PrintCircle(int [][] matrix,int col,int row,int start){
  int endrow=row-start-1;
  int endcol=col-start-1;
  // Print from left to right 1 Row 
  // No. 1 1 Row 1 It will be printed 
  for(int i=start;i<=endcol;i++){
   result.add(matrix[start][i]);
  }
  // Print from top to bottom (the first 2 Step) 
  if(endrow>start){
   for(int i=start+1;i<=endrow;i++){
    result.add(matrix[i][endcol]);
   }
  }
  // Print from right to left 3 Step) 
  if(endrow>start && endcol>start){
   for(int i=endcol-1;i>=start;i--){
    result.add(matrix[endrow][i]);
   }
  }
  // Print from bottom to top 4 Step) 
  if((endrow-start>1)&&endcol>start){
   for(int i=endrow-1;i>=start+1;i--){
    result.add(matrix[i][start]);
   }
  }
 }
}

Related articles: