给定一个 n × n 的二维矩阵表示一个图像。
将图像顺时针旋转 90 度。
说明:
你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要使用另一个矩阵来旋转图像。
通过先将数组进行左下右上对角线对称映射,在上下翻转。
class Solution {
public void rotate(int[][] matrix) {
int col = matrix.length;
int row = matrix[0].length;
for(int i = 0;i<row;i++){
for(int j = 0;j<col-i;j++){
int temp = matrix[i][j];
matrix[i][j] = matrix[row-j-1][col-i-1];
matrix[row-j-1][col-i-1] = temp;
}
}
for(int i = 0;i<row/2;i++){
for(int j = 0;j<col;j++){
int temp = matrix[i][j];
matrix[i][j] = matrix[row-i-1][j];
matrix[row-i-1][j] = temp;
}
}
}
}