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