感觉这道题没什么难度,主要得仔细!代码代码上代码,扯多了也没用
public class RotateImage {
public void rotate(int[][] matrix) {
int len = matrix.length;
int step = len - 1;
for(int i = 0;i < len / 2;i ++){
//右边界或者下边界
int rightLine = len - i - 1;
//左边界
int leftLine = i;
for(int j = i;j < rightLine;j ++){
//和第一个数交换
int col = j + step;
int row = i;
if(col > rightLine){
row = leftLine + col - rightLine;
col = rightLine;
}
swap(matrix,i,j,row,col);
//和第二个数交换
row = row + step;
if(row > rightLine){
col = rightLine - (row - rightLine);
row = rightLine;
}
swap(matrix,i,j,row,col);
//和第三个数交换
col = col - step;
if(col < leftLine){
row = rightLine - (leftLine - col);
col = leftLine;
}
swap(matrix,i,j,row,col);
}
step = step - 2;
}
}
private void swap(int[][] a,int i,int j,int row,int col){
int temp = a[i][j];
a[i][j] = a[row][col];
a[row][col] = temp;
}
}