Title Description
Enter a matrix, in order from outside to inside in a clockwise order to print out sequentially for each number, for example, if you enter the following 4 X 4 matrix: 1,234,567,891,011,121,314 15 sequentially printed out 16 digital 1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.
Solving a problem: turning cycle
. 1 public static the ArrayList <Integer> printMatrix ( int [] [] Matrix) { 2 the ArrayList <Integer> = Result new new the ArrayList <Integer> (); . 3 IF (matrix.length == 0 ) { . 4 return Result; . 5 } . 6 // number of rows and columns of the matrix initialization . 7 int row = matrix.length, column = matrix [0 ] .length; . 8 IF (column == 0 ) { . 9 // If the number of rows in the matrix is not 0, the number of columns of the matrix It is 0, meaning empty elements of the matrix, still empty list 10 return Result; . 11 } 12 is / * 13 is * loop through layers Layers are, the smaller the value determined by the rows and columns, the smaller the value -1 is an odd number in order to consider the case, 14 * is divided by 2 because the top of each loop are due to bottom left to right and right to left while the spent line 2, so only take half 15 * 0.5 + 1 in order to compensate for previous layer, and where at least one layer of the even-odd -1 condition causes 16 * / . 17 int = Layers (Math.min (Row, column) -1) / 2 +. 1 ; 18 is for ( int I = 0; I <Layers; I ++ ) { . 19 / * 20 is * from the upper left to the upper right diagonal elements starting from , since on each one from the lower left to the upper left will use up a plurality of the current row previous element, so the start j = i. 21 * traversing the index is less than the number of columns of the matrix layers subtracting the current cycle, as in previous rounds right to bottom-right element of the current row will be spent behind 22 is * * / 23 is for ( intI = J; J <column-I; J ++ ) { 24 result.add (Matrix [I] [J]); 25 } 26 is / * 27 * from the upper right to the lower right, since this round will be left to the upper right multi spent above a current column elements, so the start k = i + 1. 28 * traversing the index is less than the number of rows of the matrix layers subtracting the current cycle, because the first rounds from the lower right to the lower left element will be spent following the current column. 29 * The maximum index matrix column is column-1 (starting from 0), the elements of the filling result list column column number minus the maximum cycle index layers, 30 * as previous rounds will be spent from the upper right to the lower right of the current columns after the column element 31 is * * / 32 for ( int K = I +. 1; K <row-I; K ++ ) { 33 is result.add (Matrix [K] [column-l- I]); 34 is } 35 / * 36 * from the lower right to the lower left, the maximum index matrix column is column-1 (starting from 0), because this one to the lower right of the right element of a multi-row behind the current, combined with the previous the right wheel 37 * spent on the lower right to the back of the i th element, so the start j = column-1-i- 1. 38 * index from the lower right to the lower left of the current column during the last cycle of greater than or equal to the number of layers, because the previous rounds spent before i-1 from the lower left to the upper left column element, so j> = i. 39 * taking into account the case where the number of rows is less than the number of columns of the matrix (e.g., row 6 3), so that the maximum index of row-1 row (row number start from 0) the current cycle by subtracting the number of layers is not equal to the current cycle number of layers, 40 * otherwise, after the last round from top left to right, the next will have to repeat the same round from the lower right to the lower left elements. 41 * fill result list element row number of the row index by subtracting the maximum cycle number of layers, because the former will be spent rounds element lines below the current line from the lower left to lower right 42 is * * / 43 is for ( int J = column- I-2; (J> = I) && (= Row-I-I. 1!); J, ) { 44 is result.add (Matrix [l- Row- I] [J]); 45 } 46 is / * 47 * from the lower left to the upper left, the maximum index matrix row is row-1 (starting from 0), because it is a from the lower right to the lower left of a multi-element behind the current column, combined with the previous rounds from 48 following the i-th element * spent lower left to the upper left, so the start k = row-1-i- 1. 49 * index from the lower left to the upper left of the current line during the last cycle is greater than the number of layers, and the row j> different I =, 50 * Here, if the equal sign will eventually have to repeat this left to the upper right of a process the first element, so k> i. 51 * taking smaller than the number of columns of the matrix (e.g., 6 rows and 3 columns) in the row, so that the maximum index of the column column-1 minus the current cycle is not equal to the current cycle number of layers of layers 52 *, or a right to the final to the lower right, the next round will have to repeat the same elements from the lower left to the upper left. 53 * fill result list column number of the same elements of the current column index of a top-left to the upper right of the start of the element, are I 54 is **/ 55 for(int k=row-2-i;(k>i)&&(column-1-i!=i);k--) { 56 result.add(matrix[k][i]); 57 } 58 } 59 return result; 60 }
1 private static ArrayList<Integer> list = new ArrayList<>(); 2 public ArrayList<Integer> printMatrix01(int [][] matrix) { 3 int rows = matrix.length; 4 int columns = matrix[0].length; 5 int start = 0; 6 while(rows > start*2 && columns > start*2){ 7 printMatrixInCircle(matrix, rows, columns, start); 8 start++; 9 } 10 return list; . 11 } 12 is public static void printMatrixInCircle ( int [] [] Matrix, int rows, int Columns, int Start) { 13 is // left to right printing line 14 for ( int I = Start; I <Columns - Start; I ++ ) { 15 List.add (Matrix [Start] [I]); 16 } . 17 // top to bottom, a printing 18 is for ( int J = Start +. 1; J <rows - Start; J ++ ) { . 19 List.add ( Matrix [J] [Columns - Start -. 1 ]); 20 } 21 // right to left in the print line 22 is for ( int m = Columns - Start - 2; m> = Start && rows - Start -. 1> Start; M-- ) { 23 is List.add (Matrix [rows - Start -. 1 ] [m]); 24 } 25 // from a lower to a printing 26 is for ( int n-rows = - Start - 2; n-> Columns = Start +. 1 && - Start -. 1> Start; N-- ) { 27 List.add (Matrix [n-] [Start]); 28 } 29 }
. 1 public static the ArrayList <Integer> printMatrix02 ( int [] [] Matrix) { 2 // as a container for the result . 3 the ArrayList <Integer> = List new new <> the ArrayList (); . 4 // Number of rows of the array get accident 5 int row = matrix.length; . 6 the while (! row = 0 ) { . 7 // first row of the array to add the vessel . 8 for ( int I = 0; I <Matrix [0] .length; I ++ ) { . 9 List.add (Matrix [0 ] [I]); 10 } . 11 //When the number of lines is equal to 1 is no need to proceed, and after completion of the printing on it at the top stop 12 is IF (Row == 1 ) { 13 is BREAK ; 14 } 15 // delete array traversal above the first row, and rotating and returns this array 16 Matrix = Revert (Matrix); . 17 // number of rows updated 18 is row = matrix.length; . 19 } 20 is // return 21 is return List; 22 is } 23 is Private static int [] [] Revert ( int [ ] [] matrix) { 24 // number of rows and columns of the matrix to get 25 int rows = matrix.length; 26 int cols = the Matrix [0 ] .length; 27 // because we are the first line of the original array traversed delete, then rotate into a new array, so first look at this new initialization array 28 int [] [] = newMatrix new new int [cols] [-rows. 1 ]; 29 // this new assignment array 30 for ( int J =-cols. 1; J> = 0; J, ) { 31 is for ( int I =. 1; I <rows; I ++ ) { 32 newMatrix [-J-cols. 1] [I-. 1] = Matrix [I] [J]; 33 is } 34 is } 35 //Returns a new array 36 return newMatrix; 37 [ }
test:
1 public static void main(String[] args) { 2 int[][] matrix={{1, 2 ,3, 4},{5, 6, 7 ,8},{9 ,10 ,11 ,12 },{13 ,14, 15, 16}}; 3 ArrayList<Integer> list = printMatrix(matrix); 4 for (Integer integer : list) { 5 System.out.print(integer+" "); 6 } 7 } 8 输出:1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10