will
Note: You must rotate the image in place, which means you need to directly modify the two-dimensional matrix input. Do not use another matrix to rotate an image. Example 1 : Given Matrix = [ [ 1 , 2 , . 3 ], [ 4 , . 5 , . 6 ], [ 7 , . 8 , . 9 ] ], in situ rotation of the input matrix, so that it becomes: [ [ 7 , 4 , 1 ], [ 8 , 5 , 2 ], [ 9 , 6 , 3] ] Example 2 : Given Matrix = S [ [ . 5 , . 1 , . 9 , . 11 ], [ 2 , . 4 , . 8 , 10 ], [ 13 is , . 3 , . 6 , . 7 ], [ 15 , 14 , 12 is , 16 ] ], in situ rotation of the input matrix, so that it becomes: [ [ 15 , 13 , 2 , 5], [14, 3, 4, 1], [12, 6, 8, 9], [16, 7,10,11] ]
I found that, like the matrix transpose it over and then transform each line around it
[ 1 , 2 ], [ 00 01 ] Flip [ 00 10 ] [ 3 , 4 ] [ 10 . 11 ] [ 01 . 11 ] and around each row interchange After: [ 3 , 1 ] [ 10 00 ] [ 4 , 2 ] [ . 11 01 ] a three-dimensional matrix is the same given matrix = [ [ . 1 , 2 , . 3 ], [00 01 02 ] Flip [ 00 10 20 is ] [ 4 , 5 , 6 ], [ 10 . 11 12 is ] [ 01 . 11 21 is ] [ 7 , 8 , 9 ] [ 20 is 21 is 22 is ] [ 22 is 21 is 20 is ] ],
Then start writing code, you may be a little sleepy silly, write transpose the time I actually wrote this
for (int i=0;i<matrix.length;i++){ for (int j=0;j<matrix.length;j++){ tem = matrix[i][j]; matrix[i][j] = matrix[j][i]; matrix[j][i] = tem; //System.out.println(matrix[i][j] + " " + matrix[j][i]); } }
Then find the whole matrix as if no change had, well do not like, there is no change over ah!
Because the turn gave complete turn back, so we turn to see how, in fact, need only flip side of each diagonal on it.
[ 1 , 2 ], [ 00 01 ] Flip [ 00 10 ] [ 3 , 4 ] [ 10 . 11 ] [ 01 . 11 ] and around each row interchange After: [ 3 , 1 ] [ 10 00 ] [ 4 , 2 ] [ . 11 01 ] a three-dimensional matrix is the same given matrix = [ [ . 1 , 2 , . 3 ], [00 01 02 ] Flip [ 00 10 20 is ] [ 4 , 5 , 6 ], [ 10 . 11 12 is ] [ 01 . 11 21 is ] [ 7 , 8 , 9 ] [ 20 is 21 is 22 is ] [ 22 is 21 is 20 is ] ],
Finally, I answer:
class Solution { public void rotate(int[][] matrix) { int tem = 0; for (int i=0;i<matrix.length;i++){ for (int j=i+1;j<matrix.length;j++){ //得出i=1的过程贼搞笑 tem = matrix[i][j]; matrix[i][j] = matrix[j][i]; matrix[j][i] = tem; //System.out.println(matrix[i][j] + " " + matrix[j][i]); } } for (int i=0;i<matrix.length;i++){ for (int j=0;j<matrix.length/2;j++){ tem = matrix[i][matrix.length-1-j]; matrix[i][matrix.length-1-j] = matrix[i][j]; matrix[i][j] = tem; } } } }
The test is complete
public class S4 bywu Bear {// public void Rotate ( int [] [] Matrix) { S4 Solution = new new S4 (); int TEM = 0 ; for ( int I = 0 ; I <matrix.length; I ++ ) { for ( int J = I + 1 ; J <matrix.length; J ++) { // draw process thief i = 1 funny TEM = Matrix [I] [J]; Matrix [I] [J] = Matrix [J] [ I]; Matrix [J] [I] = tem; //System.out.println(matrix[i][j] + " " + matrix[j][i]); } } for (int i=0;i<matrix.length;i++){ for (int j=0;j<matrix.length/2;j++){ tem = matrix[i][matrix.length-1-j]; matrix[i][matrix.length-1-j] = matrix[i][j]; matrix[i][j] = tem; } } System.out.println("after"); solution.print_array(matrix); } public void print_array(int[][] arr){ for (int i=0;i<4;i++){ for (int j=0;j<4;j++){ System.out.print(" "+arr[i][j]); } System.out.println(""); } } public static void main(String[] args){ S4 solution = new S4(); int[][] matrix = { { 5, 1, 9,11}, { 2, 4, 8,10}, {13, 3, 6, 7}, {15,14,12,16} }; solution.print_array(matrix); solution.rotate(matrix); } }