You are given an n x n 2D matrix representing an image.
Rotate the image by 90 degrees (clockwise).
Follow up:
Could you do this in-place?
思路:先沿着主对角线交换矩阵内的数字,然后沿中间列交换。
public class Solution {
public void rotate(int[][] matrix) {
int rc = matrix.length;
for(int i=0;i<rc;i++){
for(int j=0;j<i;j++){
int temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
for(int j =0;j<rc/2;j++){
for(int i =0;i<rc;i++){
int temp = matrix[i][j];
matrix[i][j] = matrix[i][rc-1-j];
matrix[i][rc-1-j] = temp;
}
}
}
}