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 i = 0, j = n-1;
for (;i < j; i++, j--)
{
int forward = i, backward = j;
while (forward < j)
{
int t = matrix[i][forward];
matrix[i][forward] = matrix[backward][i];
matrix[backward][i] = matrix[j][backward];
matrix[j][backward] = matrix[forward][j];
matrix[forward][j] = t;
forward++;
backward--;
}
}
}
}