Clockwise Printing Matrix of java Programming Problem

  • 2021-07-01 07:27:37
  • OfStack

In this paper, we share the specific code of java clockwise printing matrix for your reference, the specific contents are as follows

github: Sword refers to offer programming problem


import java.util.ArrayList;

/**
 * 
 *  Sword finger offer Programming problem ( JAVA Realization ) -No. 1 19 Topic: Printing Matrix Clockwise 
 * 
 *  Title description 
 Input 1 Matrix, printing each in clockwise order from outside to inside 1 Numbers, for example, 
 If you enter the following 4 X 4 Matrix:  
1 2 3 4 
5 6 7 8 
9 10 11 12 
13 14 15 16 
 The numbers are printed in turn 1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.
 *
 */
public class Test19 {

 public static void main(String[] args) {
 int[][] array1 = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 }, { 13, 14, 15, 16 } };
 int[][] array2 = { { 1 } };
 int[][] array3 = { { 1 }, { 2 }, { 3 }, { 4 }, { 5 } };
 int[][] array4 = { { 1, 2, 3, 4, 5 } };
 int[][] array5 = { { 1, 2, 3, 4, 5 }, { 6, 7, 8, 9, 10 }, { 11, 12, 13, 14, 15 } };
 printMatrix(array1);
 System.out.println();
 printMatrix(array2);
 System.out.println();
 printMatrix(array3);
 System.out.println();
 printMatrix(array4);
 System.out.println();
 printMatrix(array5);

 }

 public static ArrayList<Integer> printMatrix(int[][] matrix) {
 ArrayList<Integer> arrayList = new ArrayList<>();
 int x = 0;
 int row = matrix.length;
 int line = matrix[matrix.length - 1].length;
 while (x < row && x < line) {
  arrayList.addAll(printLine(matrix, x, x, row, line));
  x++;
  row--;
  line--;
 }

 System.out.print(arrayList.toString());
 return arrayList;

 }

 private static ArrayList<Integer> printLine(int[][] matrix, int r1, int l1, int r2, int l2) {
 ArrayList<Integer> arrayList = new ArrayList<>();
 for (int i = l1; i <= l2 - 1; i++) {//  Ensure that the odd layer can be output in the middle 
  arrayList.add(matrix[r1][i]);
 }

 for (int k = r1 + 1; k <= r2 - 1; k++) {
  arrayList.add(matrix[k][l2 - 1]);
 }
 if ((r2 - r1) != 1) {// Prevent duplicate printing 
  for (int j = l2 - 2; j >= l1; j--) {
  arrayList.add(matrix[r2 - 1][j]);
  }
 }

 if ((l2 - l1) != 1) {// Prevent duplicate printing 
  for (int u = r2 - 2; u > r1; u--) {
  arrayList.add(matrix[u][l1]);
  }
 }

 return arrayList;
 }
}
// Other methods 
/**
import java.util.ArrayList;
public class Solution {
  public ArrayList<Integer> printMatrix(int [][] array) {
    ArrayList<Integer> result = new ArrayList<Integer> ();
    if(array.length==0) return result;
    int n = array.length,m = array[0].length;
    if(m==0) return result;
    int layers = (Math.min(n,m)-1)/2+1;// This is the number of layers 
    for(int i=0;i<layers;i++){
      for(int k = i;k<m-i;k++) result.add(array[i][k]);// Left to right 
      for(int j=i+1;j<n-i;j++) result.add(array[j][m-i-1]);// Upper right to lower right 
      for(int k=m-i-2;(k>=i)&&(n-i-1!=i);k--) result.add(array[n-i-1][k]);// Right to left 
      for(int j=n-i-2;(j>i)&&(m-i-1!=i);j--) result.add(array[j][i]);// Lower left to upper left 
    }
    return result;   
  }
}

*/


Related articles: