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) {
int d = matrix.length;
int tmp;
for (int i = 0; i < d; i++) {
for (int j = i + 1; j < d; j++) {
tmp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = tmp;
}
for (int j = 0; j < d/2; j++) {
tmp = matrix[i][j];
matrix[i][j] = matrix[i][d-1-j];
matrix[i][d-1-j] = tmp;
}
}
}
}