You are given an n x n 2D matrix representing an image.
Rotate the image by 90 degrees (clockwise).
思路:先求矩阵的转置,在按y轴对称
public class Solution {
public void rotate(int[][] matrix) {
int n = matrix.length;
for(int i = 0;i < n;i++){
for(int j = i;j < n ;j++){
int temp = 0;
temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
for(int i = 0;i < n;i++){
for(int j = 0;j < n/2;j++){
int temp = 0;
temp = matrix[i][j];
matrix[i][j] = matrix[i][n-1-j];
matrix[i][n-1-j] = temp;
}
}
}
}