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?
此题可以用3*3的矩阵来举例子,发现可以先x,y互相先对掉,然后再左右对掉,代码如下:
public class Solution {
public void rotate(int[][] matrix) {
for(int i=0;i<matrix.length;i++){
for(int j=0;j<matrix[0].length;j++){
if(i>j){
int temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
}
for(int i=0;i<matrix[0].length/2;i++){
for(int j=0;j<matrix.length;j++){
int temp = matrix[j][i];
matrix[j][i] = matrix[j][matrix[0].length-i-1];
matrix[j][matrix[0].length-i-1]=temp;
}
}
}
}