One, given a matrix, is represented by a two-dimensional array, not necessarily a phalanx (n*n), the transpose of the matrix (to the right), and the left. Like what:
1 2 3
4 5 6
7 8 9
Transpose right:
1 4 7
2 5 8
3 6 9
Another example:
1 2 3
4 5 6
Turn left
3 6
2 5
1 4
Second, realize the idea
Assuming the original matrix is m*n, the transpose becomes the n*m. Set the original matrix is arr[m][n], create a new matrix Rev[n][m]
For the right to transpose, it is the linear algebra inside of the request at, for Arr[m][n] inside each element arr[i][j], assign it to rev[j][i] can.
For turning left, for each element inside Arr[m][n] Arr[i][j], assign it to rev[n-j-1][i].
Three, complete code
Public classtest{ Public Static voidMain (string[] args) {int[] arr1 = {{1,2,3},{4,5,6},{7,8,9}}; int[] arr2 = {{1,2,3},{4,5,6}}; Reverseright (ARR1); System.out.println ("---------"); Reverseright (ARR2); System.out.println ("***********"); Reverseleft (ARR1); System.out.println ("-------------"); Reverseleft (ARR2); } Public Static voidReverseright (int[] arr) { introw =arr.length; intCol = arr[0].length; int[] Rev =New int[Col][row]; for(inti = 0; i < row; i++) { for(intj = 0; J < Col; J + +) Rev[j][i]=Arr[i][j]; } StringBuilder SB=NewStringBuilder (); //Print rotated Matrix--there are col rows and row columns for(inti = 0; I < col; i++) { for(intj = 0; J < Row; J + +) { //System.out.print (rev[i][j]+ "");Sb.append (Rev[i][j] + ""); } Sb.deletecharat (Sb.length ()-1); Sb.append ("\ n");//System.out.println ();} System.out.println (Sb.tostring ()); } Public Static voidReverseleft (int[] arr) { introw =arr.length; intCol = arr[0].length; int[] Rev =New int[Col][row]; for(inti = 0; i < row; i++) { for(intj = 0; J < Col; J + +) {Rev[col-j-1][i] =Arr[i][j]; } } //Print rotated Matrix--there are col rows and row columns for(inti = 0; I < col; i++) { for(intj = 0; J < Row; J + +) {System.out.print (Rev[i][j]+ " "); } System.out.println (); } }}
Rotation of the Matrix