# leetcode Solution 48. Rotating Image

problem
Given a two-dimensional matrix of n * n, an image is represented.
Rotate the image 90 degrees clockwise.

Explain:
You have to rotate the image in place, which means you need to modify the input two-dimensional matrix directly. Please do not use another matrix to rotate the image.
Example 1:

Given matrix=
[
[1,2,3],
[4,5,6],
[7,8,9]
],
Rotate the input matrix in place so that it becomes:
[
[7,4,1],
[8,5,2],
[9,6,3]
]
Example 2:

Given matrix=
[
[ 5, 1, 9,11],
[ 2, 4, 8,10],
[13, 3, 6, 7],
[15,14,12,16]
],

Rotate the input matrix in place so that it becomes:
[
[15,13, 2, 5],
[14, 3, 4, 1],
[12, 6, 8, 9],
[16, 7,10,11]
]
Reflection
At first I thought about the relationship between abscissa and ordinate, but finally. Hidden rules are complex, but the code is very clear and easy to write.
Diagonal substitution is done according to the negative diagonal line first, and then a conversion is made according to the middle line. You can use the example above to demonstrate in the lower brain.
You'll be surprised to find that there is such a rule. Well, how could this be thought of? No matter how you think of it, write down the answer.

Code

```package leetcode_01_50;

public class _48rotate {

public void _48rotate(int[][] matrix) {

rotatebymainline(matrix);
rotatebyHline(matrix);
}

private void rotatebymainline(int[][] matrix) {
// TODO Auto-generated method stub

for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix.length - i - 1; j++) {
int temp = matrix[i][j];
matrix[i][j] = matrix[matrix.length - j - 1][matrix.length - i - 1];
matrix[matrix.length - j - 1][matrix.length - i - 1] = temp;
}
// drawing a picture too lead u coding
}

}

private void rotatebyHline(int[][] matrix) {
// TODO Auto-generated method stub
for (int i = 0; i < matrix.length / 2; i++) {
for (int j = 0; j < matrix.length; j++) {
int temp = matrix[i][j];
matrix[i][j] = matrix[matrix.length - i - 1][j];
matrix[matrix.length - i - 1][j] = temp;
}
}

}

public static void main(String[] args) {

int[][] matrix = { { 5, 1, 9, 11 }, { 2, 4, 8, 10 }, { 13, 3, 6, 7 }, { 15, 14, 12, 16 } };

_48rotate _48rotate = new _48rotate();
_48rotate._48rotate(matrix);

}
// See the law, x-y (n-x)
// y-x direct conversion
//But it turns out that this rule has no value at all.
}
```

Posted on Sun, 06 Oct 2019 16:49:59 -0700 by Dixen