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?
Difficulty: Medium
public class Solution {
public void helper(int[][] matrix, int i, int j){
int pre = matrix[i][j];
for(int k = 0; k < 4; k++){
int temp = matrix[j][matrix.length - 1- i];
matrix[j][matrix.length - 1- i] = pre;
pre = temp;
temp = i;
i = j;
j = matrix.length - 1 - temp;
}
}
public void rotate(int[][] matrix) {
int n = matrix.length, mid = n/2 - 1;
for(int i = 0; i <= mid + n%2; i++){
for(int j = 0; j <= mid; j++){
helper(matrix, i, j);
}
}
return;
}
}