题目:
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) {
if(null==matrix||matrix.length<=0) return;
int x1=0, y1=0;
int n=matrix.length;
int x2=n-1, y2=n-1;
while(x1<x2&&y1<y2){
for(int i=x1,j=y1;j<y2;j++){
int temp = matrix[i][j];
matrix[i][j]=matrix[n-j-1][i];
matrix[n-j-1][i]=matrix[n-i-1][n-j-1];
matrix[n-i-1][n-j-1]=matrix[j][n-i-1];
matrix[j][n-i-1]=temp;
}
x1++;y1++;
x2--;y2--;
}
}
}
参考: