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?
Analysis:
public class Solution {
public void rotate(int[][] matrix) {
int row = matrix.length;
for(int layer=0; layer<row/2; layer++) {
for(int col=layer; col<row-1-layer; col++) {
int temp = matrix[layer][col];
matrix[layer][col] = matrix[row-1-col][layer]; // left -> up
matrix[row-1-col][layer] = matrix[row-1-layer][row-1-col]; // bottom -> left
matrix[row-1-layer][row-1-col] = matrix[col][row-1-layer]; // right -> bottom
matrix[col][row-1-layer] = temp; // up -> right
}
}
return;
}
}